alexkn android第一行代码-8.sqlite使用

Android自带sqlite数据库,因此常见操作应该都掌握。包括数据库的创建/升级以及增删改查。

1.创建数据库

public class MyDatabaseHelper extends SQLiteOpenHelper {

    public static final String CREATE_BOOK = "create table book ("
+ "id integer primary key autoincrement, "
+ "author text,"
+ "price real,"
+ "page integer,"
+ "name text)"; 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);
Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
}
}

创建数据库的类完成后,绑定事件触发即可

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) {
dbHelper.getWritableDatabase();
}
}); } }

2.数据库升级

假设新增一张表,

首先更新版本

dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);

然后重写onUpgrade方法

public class MyDatabaseHelper extends SQLiteOpenHelper {

    ...
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book;");
db.execSQL("drop table if exists Category");
db.execSQL(CREATE_CATEGORY);
onCreate(db);
}
}

3.添加数据(insert)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
...
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
</LinearLayout>
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 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); // 插入第二条数据
}
});
}

4.删除数据(delete)

跟3一致,除了db的调用方法不一致

db.delete("Book", "pages > ?", new String[] { "500" });

5.修改数据(update)

db.update("Book", values, "name = ?", new String[] { "The DaVinci Code" });

6.查询数据(search)

查询比较复杂,需要7个参数

alexkn android第一行代码-8.sqlite使用

        queryButton.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);
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"));
Log.d("MainActivity", "book name is " + name);
Toast.makeText(MainActivity.this,"book name is "+name,Toast.LENGTH_SHORT).show();
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();
}
});

7.原生操作

// 添加数据的方法如下:
db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)", new String[] { "The Da Vinci Code", "Dan Brown", "454", "16.96" }); db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)", new String[] { "The Lost Symbol", "Dan Brown", "510", "19.95" }); // 更新数据的方法如下:
db.execSQL("update Book set price = ? where name = ?", new String[] { "10.99", "The Da Vinci Code" }); // 删除数据的方法如下:
db.execSQL("delete from Book where pages > ?", new String[] { "500" }); // 查询数据的方法如下:
db.rawQuery("select * from Book", null);
上一篇:CC3200使用MQTT的SSL加密证书可用日期修改


下一篇:div内部实现图片旋转、放大、缩小、拖拽