界面文件activity_main.xml
<Button android:id="@+id/createButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/hello_world" android:text="create database"/> <Button android:id="@+id/updateButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/createButton" android:text="update database"/> <Button android:id="@+id/insertButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/updateButton" android:text="insert database"/> <Button android:id="@+id/updateRecordButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/insertButton" android:text="update record"/> <Button android:id="@+id/queryRecordButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/updateRecordButton" android:text="Query record"/>
DatabaseHelper.java
package com.example.sqlite_01; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper{ private static final int VERSION = 1; public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } public DatabaseHelper(Context context, String name, int version) { this(context, name, null, version); } public DatabaseHelper(Context context, String name) { this(context, name, VERSION); } //第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabase对象的时候调用 @Override public void onCreate(SQLiteDatabase db) { System.out.println("Create a database"); db.execSQL("create table user(id int, name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("Update a database"); } /* * abd shell进入当前App的目录下的databases目录下就可以看到创建的数据库 * 打开数据库sqlite3 test_db * 查看当前数据库里的表以及创建表的语句 .schema */ }
MainActivity.java
package com.example.sqlite_01; import android.support.v7.app.ActionBarActivity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends ActionBarActivity { private Button createButton = null; private Button updateButton = null; private Button insertButton = null; private Button updateRecordButton = null; private Button queryRecordButton = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createButton = (Button) findViewById(R.id.createButton); updateButton = (Button) findViewById(R.id.updateButton); insertButton = (Button) findViewById(R.id.insertButton); updateRecordButton = (Button) findViewById(R.id.updateRecordButton); queryRecordButton = (Button) findViewById(R.id.queryRecordButton); createButton.setOnClickListener(new CreateListener()); updateButton.setOnClickListener(new UpdateListener()); insertButton.setOnClickListener(new InsertListener()); updateRecordButton.setOnClickListener(new UpdateRecordListener()); queryRecordButton.setOnClickListener(new QueryListener()); } class CreateListener implements OnClickListener{ @Override public void onClick(View v) { DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db"); SQLiteDatabase db = dbHelper.getReadableDatabase();//执行了这一句才会创建数据库 } } class UpdateListener implements OnClickListener{ @Override public void onClick(View v) { DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db", 2); SQLiteDatabase db = dbHelper.getReadableDatabase();//执行了这一句才会创建数据库 } } class InsertListener implements OnClickListener{ @Override public void onClick(View v) { ContentValues contentValues = new ContentValues(); contentValues.put("id", 1); contentValues.put("name", "umgsai"); DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db", 2); SQLiteDatabase db = dbHelper.getWritableDatabase(); db.insert("user", null, contentValues); } } class UpdateRecordListener implements OnClickListener{ @Override public void onClick(View v) { DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db", 2); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", "admin"); db.update("user", values, "id = ?", new String[]{"1"}); } } class QueryListener implements OnClickListener{ @Override public void onClick(View v) { DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db", 2); SQLiteDatabase db = dbHelper.getReadableDatabase(); //db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy) Cursor cursor = db.query("user", new String[]{"id", "name"}, "id = ?", new String[]{"1"}, null, null, "id"); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); System.out.println("name>>>" + name); } } } }
可以通过adb shell命令来查看数据库
>abd shell
>ls -l (查看当前目录下的文件及文件夹)
>cd data (进入data目录)
>ls -l (查看当前目录下的文件及文件夹)
>cd data (进入data目录)
>ls -l (查看当前目录下的文件及文件夹)
>cd com.example.sqlite_01 (进入当前项目目录)
>ls -l (查看当前目录下的文件及文件夹)
>cd databases (进入databases目录)
>ls -l (此时就可以看到数据库文件test_db)
>sqlite3 test_db (使用sqlite打开test_db数据库)
>.schema (查看当前数据库中的表以及建表的语句)
>select * from user; (从user表中查询数据)