近期学期安卓开发,学习SQLite的基本使用,现在分享一下使用SQLite的基本用法
开发工具:Android Stadio3.3.1
1、项目结构
2、继承自SQLiteOpenHelper的MyDatabaseHelper.java文件,创建数据库,创建表都在该文件。
package com.example.sqlitetest.db;
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_name_code integer)";
private Context mContext;
public MyDatabaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version){
super(context,name,factory,version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_LONG).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);
}
}
3、继承自AppCompatActivity的MainActivity.java文件,对数据增删改查都在该文件。
package com.example.sqlitetest;
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;
import com.example.sqlitetest.db.MyDatabaseHelper;
import java.util.logging.Logger;
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,3);
Button createDabase = findViewById(R.id.create_database);
createDabase.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
Button addBtn = findViewById(R.id.add_data);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
ContentValues values = new ContentValues();
//开始组装第一条数据
values.put("name","The Da Vince Code");
values.put("author","Dan Brown");
values.put("pages","452");
values.put("price","25.9");
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","93.9");
db.insert("Book",null,values);
}
});
Button updateData = 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.89);
db.update("Book",values,"name=?",new String[]{
"The Da Vince Code"
});
}
});
Button deletBtn = findViewById(R.id.delete_data);
deletBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book","pages>? ",new String[]{
"500"
});
}
});
Button queryBtn = findViewById(R.id.query_data);
queryBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book",null,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.e("Main","book name="+name);
Log.e("Main","book author="+author);
Log.e("Main","book pages="+pages);
Log.e("Main","book price="+price);
}while (cursor.moveToNext());
cursor.close();
}
}
});
}
}
4、Activity_main.xml文件,前端展示页面,效果如下图
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/create_database"
android:text="创建数据库"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/add_data"
android:text="添加数据"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/update_data"
android:text="更新数据"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/delete_data"
android:text="删除数据"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/query_data"
android:text="查询数据"/>
</LinearLayout>
注:为了方便使用的伙伴能看到创建的数据库表和数据,本人有相应的工具SQLiteStudio.exe,网上也有下载,也可以直接从我这里下载。