在Android中使用SQLite數(shù)據(jù)庫及其操作詳解
1. 創(chuàng)建數(shù)據(jù)庫表
首先,我們需要定義數(shù)據(jù)庫表的結(jié)構(gòu)。在 Android 中,這通常通過 SQL 語句來實現(xiàn)。在 SQLiteOpenHelper 的 onCreate() 方法中執(zhí)行 SQL 語句來創(chuàng)建表。
String sql = "create table notepad(id integer primary key autoincrement, content text, time text)"; db.execSQL(sql);
id是主鍵,類型為INTEGER,并且通過AUTOINCREMENT自動遞增。content是用于存儲筆記內(nèi)容的文本字段。time是用于存儲時間信息的文本字段。
當應用程序第一次運行時,數(shù)據(jù)庫會通過執(zhí)行這條 CREATE TABLE 語句來創(chuàng)建 notepad 表。
2. 插入數(shù)據(jù)到數(shù)據(jù)庫
接下來,我們定義一個方法,將數(shù)據(jù)插入到 notepad 表中。我們使用 execSQL() 方法來執(zhí)行 INSERT 語句。
public void insertNotepad(SQLiteDatabase db, String content, String time) {
String sql = "insert into notepad (content, time) values (?, ?)";
db.execSQL(sql, new Object[]{content, time});
}
insert into notepad (content, time):指定要插入的表名和列名。values (?, ?):占位符?用于安全地插入數(shù)據(jù)。這個寫法能有效防止 SQL 注入攻擊。new Object[]{content, time}:創(chuàng)建一個Object數(shù)組,將content和time作為參數(shù)傳遞,依次替換 SQL 語句中的占位符?。
該方法會將指定的筆記內(nèi)容和時間插入到 notepad 表中,數(shù)據(jù)庫會自動生成唯一的 id 值。
3. 執(zhí)行查詢操作
我們還需要從數(shù)據(jù)庫中查詢數(shù)據(jù)。下面的方法用于根據(jù) id 查詢 notepad 表中的記錄。
public Cursor getNotepad(SQLiteDatabase db, int id) {
return db.rawQuery("select * from notepad where id = ?", new String[]{String.valueOf(id)});
}
select * from notepad where id = ?:這是一條SELECT查詢語句,用于檢索id等于特定值的記錄。?是占位符。new String[]{String.valueOf(id)}:將id轉(zhuǎn)換為字符串,并通過數(shù)組傳遞給 SQL 語句以替換占位符?。
調(diào)用此方法時,會返回一個 Cursor 對象,它包含查詢到的結(jié)果集。
4. 驗證查詢結(jié)果
查詢到結(jié)果后,我們可以使用 Cursor 對象來遍歷結(jié)果并提取字段數(shù)據(jù)。
Cursor cursor = dbHelper.getNotepad(db, 1);
System.out.println("Query executed for record with id 1.");
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String content = cursor.getString(cursor.getColumnIndex("content"));
String time = cursor.getString(cursor.getColumnIndex("time"));
System.out.println("Record found: ID = " + id + ", Content = " + content + ", Time = " + time);
assertEquals(1, id);
assertEquals("Test content", content);
assertEquals("2024-08-23 12:00", time);
} else {
System.out.println("No record found.");
}
cursor.moveToFirst():將Cursor移動到第一條記錄。如果有記錄存在,則返回true。cursor.getInt(cursor.getColumnIndex("id")):通過列名獲取id列的值。cursor.getString(cursor.getColumnIndex("content"))和cursor.getString(cursor.getColumnIndex("time")):分別獲取content和time列的值。
通過這些方法,我們可以提取查詢結(jié)果并輸出到控制臺。同時,使用 assertEquals 方法來驗證結(jié)果是否與預期一致。
5. 參數(shù)化查詢的安全性
在上述的 SQL 查詢語句中,我們使用了占位符 ?,并將實際參數(shù)通過數(shù)組傳遞到 SQL 語句中。這是一種參數(shù)化查詢的方式,可以有效防止 SQL 注入攻擊。通過參數(shù)化查詢,你無需手動拼接 SQL 語句,可以確保輸入的數(shù)據(jù)不會被惡意利用來修改 SQL 邏輯。
例如:
String sql = "select * from notepad where id = ?";
db.rawQuery(sql, new String[]{String.valueOf(id)});
這里的 new String[]{String.valueOf(id)} 創(chuàng)建了一個字符串數(shù)組,其中包含 id 的字符串形式。這個數(shù)組會替換 SQL 語句中的占位符 ?。
6. 代碼總結(jié)
通過這篇文章的示例,我們展示了如何在 Android 中使用 SQLite 數(shù)據(jù)庫,創(chuàng)建表、插入數(shù)據(jù)、查詢數(shù)據(jù),并對查詢結(jié)果進行驗證。以下是代碼的完整實現(xiàn):
public class MyDbHelper extends SQLiteOpenHelper {
public MyDbHelper(Context context) {
super(context, "mydatabase.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table notepad(id integer primary key autoincrement, content text, time text)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS notepad");
onCreate(db);
}
public void insertNotepad(SQLiteDatabase db, String content, String time) {
String sql = "insert into notepad (content, time) values (?, ?)";
db.execSQL(sql, new Object[]{content, time});
}
public Cursor getNotepad(SQLiteDatabase db, int id) {
return db.rawQuery("select * from notepad where id = ?", new String[]{String.valueOf(id)});
}
}
通過這個簡單的示例,你可以掌握在 Android 應用中如何使用 SQLite 來執(zhí)行數(shù)據(jù)庫操作。你可以進一步擴展這些代碼來實現(xiàn)更多功能,比如更新和刪除記錄、復雜查詢等。
7. 小結(jié)
SQLite 是 Android 中非常強大且輕量的內(nèi)置數(shù)據(jù)庫引擎,適合處理小型的持久化數(shù)據(jù)。通過本文的代碼示例,你可以快速上手并在實際項目中使用 SQLite 來管理和操作數(shù)據(jù)。在實際開發(fā)中,務必采用參數(shù)化查詢等安全手段來防止 SQL 注入攻擊,確保數(shù)據(jù)的安全性。
如果你有更復雜的需求,例如需要處理大量數(shù)據(jù)、關(guān)系復雜的數(shù)據(jù)操作,可能需要考慮其他數(shù)據(jù)庫解決方案,如使用 Room 或者將數(shù)據(jù)存儲到云端。希望本文能夠幫助你更好地理解 SQLite 在 Android 中的使用,提升開發(fā)效率和數(shù)據(jù)管理能力。
到此這篇關(guān)于在Android中使用SQLite數(shù)據(jù)庫及其操作詳解的文章就介紹到這了,更多相關(guān)Android中使用SQLite內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android RadioButton和CheckBox組件的使用方法
本次實驗中主要是學習如何使用RadioGroup,CheckBox,RadioButton和Toast這幾個控件,android UI開發(fā)中也會經(jīng)常用到他們2013-11-11
Android Intent傳遞數(shù)據(jù)大小限制詳解
這篇文章主要給大家介紹了關(guān)于Android Intent傳遞數(shù)據(jù)大小限制的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04
Android使用MediaRecorder實現(xiàn)錄音及播放
這篇文章主要為大家詳細介紹了Android使用MediaRecorder實現(xiàn)錄音及播放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Fultter NestedScrollView實現(xiàn)吸頂效果以及遇到問題解析
這篇文章主要為大家介紹了Fultter NestedScrollView實現(xiàn)吸頂效果以及遇到問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09

