使用原生sql操作数据库
新增数据:db.execSQL("insert into Book (name, author, pages, prices) values (?,?,?,?)", new String[] {"love", "guanjia", "520", "25.5"});
更新数据:db.execSQL("update Book set price = ? where name = ?", new String[] {"26.6", "love"});
删除数据:db.execSQL("delete from Book where pages > ", new String[] {"100"});
查询数据:db.rawQuery("select * from Book ", new String[] {"100"});
使用 封装好的东西来操作数据库
package
com.example.databasetest;
import
android.content.Context;
import
android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteOpenHelper;
import
android.widget.Toast;
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;
}
@Override
//这里的onCreate方法,如果数据库存在,那么就只会执行一次。
public void
onCreate(SQLiteDatabase db) {
db.execSQL(
CREATE_BOOK
);
db.execSQL(
CREATE_CATEGORY
);
Toast.
makeText
(
mContext
,
"Create succeeded"
, Toast.
LENGTH_SHORT
).show();
}
@Override
//这里是在实例化这个dbhelp的时候,最后一个数字表示版本号,如果大于上次设置的版本号,就会执行
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);
}
}
package
com.example.databasetest;
import
android.content.ContentValues;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.widget.Button;
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
,
1
);
Button createDatabase = (Button) findViewById(R.id.
create_database
);
createDatabase.setOnClickListener(
new
View.OnClickListener() {
@Override
public void
onClick(View v) {
//getWritableDatabase() 和 getReadableDatabase() 都可以创建或实例化一个数据库对象
//当数据库不可写入(例如内存满了),getReadableDatabase返回只读对象,但是getWritableDatabase会报错
dbHelper
.getWritableDatabase();
}
});
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.clear();
// 开始组装第二条数据
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 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"
,
10.99
);
//数据库名, 新数据, where语句, 后面是条件
db.update(
"Book"
, values,
"name = ?"
,
new
String[] {
"The Da Vinci Code"
});
}
});
//删除数据
Button deleteButton = (Button) findViewById(R.id.
delete_data
);
deleteButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public void
onClick(View v) {
SQLiteDatabase db =
dbHelper
.getWritableDatabase();
db.delete(
"Book"
,
"pages > ?"
,
new
String[] {
"500"
});
}
});
//查询数据
Button queryButton = (Button) findViewById(R.id.
query_data
);
queryButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public void
onClick(View v) {
SQLiteDatabase db =
dbHelper
.getWritableDatabase();
// 查询Book表中所有的数据,参数分别是是表名,列名, where条件,具体值, groupby, having, order by
//查询表中所有数据,并循环
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
(
"MainActivity"
,
"book name is "
+ name);
Log.
d
(
"MainActivity"
,
"book author is "
+ author);
Log.
d
(
"MainActivity"
,
"book pages is "
+ pages);
Log.
d
(
"MainActivity"
,
"book price is "
+ price);
}
while
(cursor.moveToNext());
}
cursor.close();
}
});
}
}