SQLite
SQLiteActivity
package org.wp.activity; import org.wp.db.DatabaseHelper; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; /** * adb访问SQLite * cmd --> adb * adb shell * * ls -l --> cd data --> ls -l --> cd data --> ls -l --> cd org.wp.activity --> ls * cd --> databases * SQLite sqlite_db * .schema * select * from user; * * @author wp * */ public class SQLiteActivity extends Activity { private static final String TAG = "SQLiteActivity"; private Button createDBButton; private Button updateDBButton; private Button insertButton; private Button updateButton; private Button deleteButton; private Button queryButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); createDBButton = (Button) findViewById(R.id.createDatabase); updateDBButton = (Button) findViewById(R.id.updateDatabase); insertButton = (Button) findViewById(R.id.insert); updateButton = (Button) findViewById(R.id.update); deleteButton = (Button) findViewById(R.id.delete); queryButton = (Button) findViewById(R.id.query); createDBButton.setOnClickListener(listener); updateDBButton.setOnClickListener(listener); insertButton.setOnClickListener(listener); updateButton.setOnClickListener(listener); deleteButton.setOnClickListener(listener); queryButton.setOnClickListener(listener); } private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View arg0) { Button button = (Button) arg0; switch (button.getId()) { case (R.id.createDatabase): // 创建一个DatabaseHelper对象 DatabaseHelper dbHelper1 = new DatabaseHelper( SQLiteActivity.this, "sqlite_db"); // 只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库 SQLiteDatabase db1 = dbHelper1.getReadableDatabase(); break; case (R.id.updateDatabase): DatabaseHelper dbHelper2 = new DatabaseHelper( SQLiteActivity.this, "sqlite_db", 2); SQLiteDatabase db2 = dbHelper2.getReadableDatabase(); break; case (R.id.insert): DatabaseHelper dbHelper3 = new DatabaseHelper( SQLiteActivity.this, "sqlite_db"); SQLiteDatabase db3 = dbHelper3.getWritableDatabase(); // 生成ContentValues对象 ContentValues values1 = new ContentValues(); // 向该对象当中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须和数据库当中的数据类型一致 values1.put("id", 1); values1.put("name", "某某某1"); values1.put("age", 25); // 调用insert方法,就可以将数据插入到数据库当中 db3.insert("user", null, values1); values1.put("id", 2); values1.put("name", "某某某2"); values1.put("age", 10); // 调用insert方法,就可以将数据插入到数据库当中 db3.insert("user", null, values1); break; case (R.id.delete): DatabaseHelper dbHelper4 = new DatabaseHelper( SQLiteActivity.this, "sqlite_db"); SQLiteDatabase db4 = dbHelper4.getWritableDatabase(); db4.delete("user", "id = ?", new String[] { "1" }); break; case (R.id.update): DatabaseHelper dbHelper5 = new DatabaseHelper( SQLiteActivity.this, "sqlite_db"); SQLiteDatabase db5 = dbHelper5.getWritableDatabase(); ContentValues values2 = new ContentValues(); values2.put("name", "张三1"); values2.put("age", 33); db5.update("user", values2, "id = ?", new String[] { "1" }); values2.put("name", "张三2"); values2.put("age", 15); db5.update("user", values2, "id = ?", new String[] { "2" }); break; case (R.id.query): DatabaseHelper dbHelper6 = new DatabaseHelper( SQLiteActivity.this, "sqlite_db"); SQLiteDatabase db6 = dbHelper6.getReadableDatabase(); Cursor cursor = db6.query("user", new String[] { "id", "name", "age" }, "id = ?", new String[] { "1" }, null, null, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor .getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.i(TAG, "count:" + cursor.getCount()); Log.i(TAG, "columnCount:" + cursor.getColumnCount()); Log.i(TAG, "id:" + id + ",name:" + name + ",age:" + age); } break; } } }; }
DatabaseHelper
package org.wp.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.util.Log; /** * DatabaseHelper作为一个访问SQLite的助手类,提供两个方面的功能 * 第一: getReadableDatabase(),getWritableDatabase() * 可以获得SQLiteDatabse对象,通过该对象可以对数据库进行操作 * 第二: 提供了onCreate()和onUpgrade()两个回调函数 * 允许我们在创建和升级数据库时,进行自己的操作 * * @author wp * */ public class DatabaseHelper extends SQLiteOpenHelper { private static final String TAG = "DatabaseHelper"; private static final int VERSION = 1; public DatabaseHelper(Context context, String name) { this(context, name, VERSION); } public DatabaseHelper(Context context, String name, int version) { this(context, name, null, version); } // 在SQLiteOpenHelper的子类当中,必须有该构造函数 public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { // 必须通过super调用父类当中的构造函数 super(context, name, factory, version); } // 该函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabse对象的时候,才会调用这个方法 @Override public void onCreate(SQLiteDatabase db) { Log.i(TAG, "Create Database"); db.execSQL("create table user(id int,name varchar(20),age int)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i(TAG, "Update Database"); } }
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:id="@+id/createDatabase" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="createDatabase" /> <Button android:id="@+id/updateDatabase" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="updateDatabase" /> <Button android:id="@+id/insert" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="insert" /> <Button android:id="@+id/update" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="update" /> <Button android:id="@+id/delete" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="delete" /> <Button android:id="@+id/query" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="query" /> </LinearLayout>