`
fonter
  • 浏览: 858318 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

android之sql例子

阅读更多

一个android操作sql例子

package jp.javadrive.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import java.io.FileNotFoundException;
import android.database.SQLException;
import android.content.ContentValues;
import android.util.Log;
import android.database.sqlite.SQLiteCursor;

public class Test01_01 extends Activity implements OnClickListener{
    private final int FP = ViewGroup.LayoutParams.FILL_PARENT; 
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 

    private Button buttonInsert;
    private Button buttonDisp;
    private EditText editName;
    private EditText editPrice;

    private SQLiteDatabase db;

    private int DB_VERSION = 1;
    private int DB_MODE = Context.MODE_PRIVATE;
    private String DB_NAME = "db_select_01";
    private String TABLE_NAME = "product";
    private TextView textResult;

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        db = null;

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);


        LinearLayout dataLayout = new LinearLayout(this);
        dataLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(dataLayout, createParam(WC, WC));

        TextView textName = new TextView(this);
        textName.setText("name");
        dataLayout.addView(textName, createParam(WC, WC));

        editName = new EditText(this);
        editName.setWidth(80);
        dataLayout.addView(editName, createParam(WC, WC));

        TextView textPrice = new TextView(this);
        textPrice.setText("price");
        dataLayout.addView(textPrice, createParam(WC, WC));

        editPrice = new EditText(this);
        editPrice.setWidth(80);
        dataLayout.addView(editPrice, createParam(WC, WC));


        LinearLayout buttonLayout = new LinearLayout(this);
        buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(buttonLayout, createParam(WC, WC));

        buttonInsert = new Button(this);
        buttonInsert.setText("INSERT");
        buttonInsert.setOnClickListener(this);
        buttonLayout.addView(buttonInsert, createParam(WC, WC));


        LinearLayout listLayout = new LinearLayout(this);
        listLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(listLayout, createParam(WC, WC));

        buttonDisp = new Button(this);
        buttonDisp.setText("Disp Data");
        buttonDisp.setOnClickListener(this);
        listLayout.addView(buttonDisp, createParam(WC, WC));

        textResult = new TextView(this);
        textResult.setText("");
        listLayout.addView(textResult, createParam(WC, WC));


        openDatabase();
    }

    private LinearLayout.LayoutParams createParam(int w, int h){
        return new LinearLayout.LayoutParams(w, h);
    }

    private void openDatabase(){
        try {
            db = openDatabase(DB_NAME, null);
        } catch (FileNotFoundException e) {
            try {
                db = createDatabase(DB_NAME, DB_VERSION, DB_MODE, null);
                createTable();
            } catch (FileNotFoundException e2) {
                db = null;
                Log.e("ERROR", e2.toString());
            }
        }
    }

    private void createTable(){
        String sql = "create table " + TABLE_NAME + " ("
            + "id integer primary key autoincrement, "
            + "name text not null, "
            + "price integer);";

        try {
            db.execSQL(sql);
        } catch (SQLException e) {
            Log.e("ERROR", e.toString());
        }
    }

    public void onClick(View v) {
        if (v == buttonInsert){
            String name =  editName.getText().toString();
            String price =  editPrice.getText().toString();

            ContentValues cv = new ContentValues();
            cv.put("name", name);
            cv.put("price", price);
            db.insert(TABLE_NAME, null, cv);

            editName.setText("");
            editPrice.setText("");
        }else if (v == buttonDisp){
            String sql = "select * from " + TABLE_NAME + ";";

            try {
                SQLiteCursor c = (SQLiteCursor)db.query(sql, null);

                int rowcount = c.count();
                StringBuffer sb = new StringBuffer();
                c.first();

                for (int i = 0; i < rowcount ; i++) {
                    int id = c.getInt(0);
                    String name = c.getString(1);
                    int price = c.getInt(2);

                    sb.append("[" + id + "," + name + "," + price + "]\n");

                    c.next();
                }

                textResult.setText(new String(sb));
            } catch (SQLException e) {
                Log.e("ERROR", e.toString());
            }
        }
    }
}

 

android之显示Log

androd之绘制文本(FontMetrics

android之获取信息终端

iWidsets 发布1.8.1版本(20090920)

java多线程设计wait/notify机制 (synchronized与对象锁)

android下的创建和读取资源文件

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics