创建数据库
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book("
+"id integer primary key autoincrement,"
+"autor text,"
+"price real,"
+"pages integer,"
+"name text)";
private Context mContext;
public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
//执行SQL语句
db.execSQL(CREATE_BOOK);
Toast.makeText(mContext,"Create succeed",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
Button button1 = findViewById(R.id.button_read);
Button button2 = findViewById(R.id.button_table);
final EditText editText = findViewById(R.id.edit_text);
final TextView textView = findViewById(R.id.text_view);
final MyDatabaseHelper dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,1);
//创建数据库和表
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用该方法,线查看是否有BookStroe数据库,如果没有,就创建,并调用onCreate()方法,创建数据库表
dbHelper.getWritableDatabase();
}
});
}
}
如果还要再创建一个表时,不能直接在onCreate()里面添加,因为已经创建了一个数据库,不会再创建数据库,不会执行onCreate()方法,因此需要重写onUpgrade()方法,并该传入的版本号
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book("
+"id integer primary key autoincrement,"
+"autor text,"
+"price real,"
+"pages integer,"
+"name text)";
//创建第二个表
public static final String CREATE_CATEGORY = "create table Catagory("
+"id integer primary key autoincrement,"
+"category_name text," +
"category_code integer)";
private Context mContext;
public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
//执行SQL语句
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"Create succeed",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);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
Button button1 = findViewById(R.id.button_read);
Button button2 = findViewById(R.id.button_table);
final EditText editText = findViewById(R.id.edit_text);
final TextView textView = findViewById(R.id.text_view);
final MyDatabaseHelper dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
//创建数据库和表
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用该方法,线查看是否有BookStroe数据库,如果没有,就创建,并调用onCreate()方法,创建数据库表
dbHelper.getWritableDatabase();
}
});
}
}
数据的增删查改
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:lines="20"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="存储"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="读取"
android:id="@+id/button_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="创建数据库表"
android:id="@+id/button_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="删除"
android:id="@+id/button_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="更新"
android:id="@+id/button_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
Button button1 = findViewById(R.id.button_read);
Button button2 = findViewById(R.id.button_table);
Button update = findViewById(R.id.button_update);
Button delete = findViewById(R.id.button_delete);
final TextView textView = findViewById(R.id.text_view);
final MyDatabaseHelper dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,3);
//创建数据库和表
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用该方法,线查看是否有BookStroe数据库,如果没有,就创建,并调用onCreate()方法,创建数据库表
dbHelper.getWritableDatabase();
}
});
//存储数据
button.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);
values.clear();
values.put("name","hello,world");
values.put("author","Dan Brown");
values.put("pages",250);
values.put("price",20.95);
db.insert("Book",null,values);
}
});
//查询数据
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("");
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book",null,null,
null,null,null,null);
if(cursor.moveToFirst()){
do{
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"));
textView.append(name+" "+author+" "+pages+" "+price+"\n");
}while(cursor.moveToNext());
}
cursor.close();
}
});
//更新数据
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price",10.99);
db.update("Book",values,"name=?",new String[]{"The Da Vinci Code"});
}
});
//删除数据
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
//删除页数大于500的
db.delete("Book","pages > ?",new String[]{"500"});
}
});
}
}