创建
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/create_database"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Create database"/></LinearLayout>
main
package demo.jq.com.databasetest;import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;/*** @author jim*/
public class MainActivity extends AppCompatActivity {/*** 引入数据库助手类*/private MyDatabaseHelper dbHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);// 初始化创建按钮Button createDatabase = (Button) findViewById(R.id.create_database);// 设置点击事件createDatabase.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {// 执行写入数据库操作dbHelper.getWritableDatabase();}});}
}
helper
package demo.jq.com.databasetest;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;/*** @author Jim*/public class MyDatabaseHelper extends SQLiteOpenHelper {public static final String CREATE_BOOK = "create table Book("+ "id integer primary key autoincrement,"+ "author text,"+ "price real," // 浮点型+ "pages integer,"+ "name text)"; // 文本类型public static final String CREATE_CATEGORY = "create table Category ("+ "id integer primary key autoincrement,"+ "category_name text,"+ "category_code integer)";private Context mContext;public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version) {super(context,name,factory,version);mContext = context;}/*** 数据库创建的时候,才执行* 如果数据库已经存在,将不会执行这个方法* @param db*/@Overridepublic void onCreate(SQLiteDatabase db) {// 执行sql语句db.execSQL(CREATE_BOOK);db.execSQL(CREATE_CATEGORY);Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("drop table if exists Book");db.execSQL("drop table if exists Category");onCreate(db);}
}
添加数据
<Buttonandroid:id="@+id/add_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Add data"/>
// 初始化添加数据按钮Button addData = (Button) findViewById(R.id.add_data);addData.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();ContentValues values = new ContentValues();// 开始组装第一条数据values.put("name","The Da Vinci Code");values.put("author","Dan Brown");values.put("pages",454);values.put("price",16.96);// 插入第一条数据db.insert("Book",null,values);// 开始组装第二条数据values.put("name","The Lost Symbol");values.put("author","Dan Brown");values.put("pages",510);values.put("price",19.95);// 插入第二条数据db.insert("Book",null,values);}});
修改数据
<Buttonandroid:id="@+id/update_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Update data"/>
// 更改数据Button updateData = (Button) findViewById(R.id.update_data);updateData.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();ContentValues values = new ContentValues();values.put("price",18.95);// 修改数据db.update("Book",values,"id = ?",new String[] {"1"});}});
删除数据
<Buttonandroid:id="@+id/delete_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Delete data"/>
// 删除数据Button deleteData = (Button) findViewById(R.id.delete_data);deleteData.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();// 删除数据db.delete("Book","pages > ?",new String[] {"500"});}});
查询数据
<Buttonandroid:id="@+id/query_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Query data"/>
// 查询数据Button queryData = (Button) findViewById(R.id.query_data);queryData.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();// 查询Book表中所有的数据Cursor cursor = db.query("Book",null,null,null,null,null,null);if (cursor.moveToFirst()) {do {// 遍历Cursor对象,取出数据String name = cursor.getString(cursor.getColumnIndex("name"));String author = cursor.getString(cursor.getColumnIndex("author"));int pages = cursor.getInt(cursor.getColumnIndex("pages"));double price = cursor.getDouble(cursor.getColumnIndex("price"));Log.d(TAG,"book name is "+name);Log.d(TAG,"book author is "+author);Log.d(TAG,"book pages is "+pages);Log.d(TAG,"book price is "+price);} while (cursor.moveToNext());}cursor.close();}});