创建
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">
<Button
android: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;
@Override
protected 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(){
@Override
public 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
*/
@Override
public void onCreate(SQLiteDatabase db) {
// 执行sql语句
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
添加数据
<Button
android: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(){
@Override
public 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);
}
});
修改数据
<Button
android: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(){
@Override
public 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"});
}
});
删除数据
<Button
android: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(){
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
// 删除数据
db.delete("Book","pages > ?",new String[] {"500"});
}
});
查询数据
<Button
android: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(){
@Override
public 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();
}
});
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/7698573.html,如需转载请自行联系原作者