android之sql例子

本文提供了一个 Android 应用中操作 SQLite 数据库的示例代码。该示例展示了如何创建数据库及表,插入数据,并从数据库中查询数据进行展示。

一个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下的创建和读取资源文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值