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

更多相关文章

  1. Android实现书籍翻页效果--扩展版
  2. Android实现书籍翻页效果--扩展版(转)
  3. Android(安卓)OpenGL入门
  4. Android学习札记15:对Android中View绘制流程的一些理解
  5. Android(安卓)N中UI硬件渲染(hwui)的HWUI_NEW_OPS(基于Android(安
  6. Android请求获取Java后端数据,登录界面例子
  7. Android贝塞尔曲线————波浪效果(大波浪)
  8. Android自定义视图二:如何绘制内容
  9. Android自定义视图二:如何绘制内容

随机推荐

  1. Android 自定义TextView去除paddingTop和
  2. android studio 更新3.0后多渠道打包配置
  3. (Android)搭建NDK开发环境 (二)
  4. Android音量相关知识总结(三)调节音量流程
  5. Android命令行获取WiFi列表以及参数
  6. Activity 与 ActivityManagerService 的
  7. Android(安卓)设置图片 Bitmap任意透明度
  8. HasMap 与 SparseArray
  9. Android中的AOP
  10. 最简单的android底部导航栏 + Fragment的