main.xml文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:id="@+id/create"    android:text="创建数据库"    /><Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:id="@+id/upgrade"    android:text="更新数据库"    /><Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:id="@+id/insert"    android:text="插入数据"    /><Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:id="@+id/update"    android:text="修改数据"    /><Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:id="@+id/select"    android:text="查询数据"    /></LinearLayout>

继承自SQLiteOpenHelper的MyOpenHleper类:
package com.android.danny.hleper;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class MyOpenHleper extends SQLiteOpenHelper {public static final int VERSION =1;public MyOpenHleper(Context context, String name) {super(context, name, null, VERSION);// TODO Auto-generated constructor stub}public MyOpenHleper(Context context, String name,int ver) {super(context, name, null, ver);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {System.out.println("------Create Database----");db.execSQL("CREATE TABLE person (id int ,name varchar(20))");}@Overridepublic void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {// TODO Auto-generated method stubSystem.out.println("------Upgrade Database----");}}

SQLite数据操作类:
package com.android.danny.db;import com.android.danny.hleper.MyOpenHleper;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Sqlite3 extends Activity implements OnClickListener{    Button create;Button upgrade;Button insert;Button update;Button select;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                create = (Button)findViewById(R.id.create);        upgrade = (Button)findViewById(R.id.upgrade);        insert = (Button)findViewById(R.id.insert);        update = (Button)findViewById(R.id.update);        select = (Button)findViewById(R.id.select);                create.setOnClickListener(this);        upgrade.setOnClickListener(this);        insert.setOnClickListener(this);        update.setOnClickListener(this);        select.setOnClickListener(this);    }@Overridepublic void onClick(View v) {MyOpenHleper dbDatabase;if (v == create) {dbDatabase = new MyOpenHleper(this, "test_db.db");//创建数据库dbDatabase.getWritableDatabase();}else if (v == upgrade) {//更新数据库dbDatabase = new MyOpenHleper(this, "test_db.db",2);dbDatabase.getReadableDatabase();}else if (v == insert) {//插入数据ContentValues values = new ContentValues();values.put("id", 1);values.put("name", "danny");dbDatabase = new MyOpenHleper(this, "test_db.db");SQLiteDatabase database = dbDatabase.getWritableDatabase();database.insert("person", null, values);}else if (v == update) {//修改数据ContentValues values = new ContentValues();values.put("name", "danny&kiki");dbDatabase = new MyOpenHleper(this, "test_db.db");SQLiteDatabase database = dbDatabase.getWritableDatabase();database.update("person", values, "id=?", new String[] {"1"});}else if (v == select) {//数据查询操作dbDatabase = new MyOpenHleper(this, "test_db.db");SQLiteDatabase database = dbDatabase.getReadableDatabase();Cursor cursor = database.query("person", new String[] {"id","name"},"id=?", new String[] {"1"}, null, null, null);while (cursor.moveToNext()) {String name = cursor.getString(cursor.getColumnIndex("name"));System.out.println("query:----->"+name);}}}}

更多相关文章

  1. Android 获取imu数据
  2. android操作ini工具类
  3. android创建数据库和表
  4. Android解析json数据
  5. Android滚动加载数据
  6. Android在listview添加checkbox实现批量操作问题
  7. 21、从头学Android之Android的数据存储--SD卡
  8. Android NDK开发之Jni的数据类型

随机推荐

  1. 试玩Android(一)
  2. Android随笔之——Android ADB详解
  3. Android Native C/C++简介
  4. Android Dialog对话框的七种形式的使用
  5. Android中的布局方式(二)
  6. Android在TQ2440开发板上的移植
  7. android声音播放
  8. Android用Application设置全局变量以及使
  9. Android之Adapter用法总结
  10. Android学习笔记之Android包、ADB介绍