在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
是用于存儲時間信息的文本字段。
當(dāng)應(yīng)用程序第一次運行時,數(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é)果是否與預(yù)期一致。
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)建了一個字符串?dā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 應(yīng)用中如何使用 SQLite 來執(zhí)行數(shù)據(jù)庫操作。你可以進一步擴展這些代碼來實現(xiàn)更多功能,比如更新和刪除記錄、復(fù)雜查詢等。
7. 小結(jié)
SQLite 是 Android 中非常強大且輕量的內(nèi)置數(shù)據(jù)庫引擎,適合處理小型的持久化數(shù)據(jù)。通過本文的代碼示例,你可以快速上手并在實際項目中使用 SQLite 來管理和操作數(shù)據(jù)。在實際開發(fā)中,務(wù)必采用參數(shù)化查詢等安全手段來防止 SQL 注入攻擊,確保數(shù)據(jù)的安全性。
如果你有更復(fù)雜的需求,例如需要處理大量數(shù)據(jù)、關(guān)系復(fù)雜的數(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)文章
解決EditText編輯時hint 在6.0 手機上顯示不出來的問題
下面小編就為大家?guī)硪黄鉀QEditText編輯時hint 在6.0 手機上顯示不出來的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Android程序報錯程序包org.apache.http不存在問題的解決方法
這篇文章主要介紹了Android程序報錯"程序包org.apache.http不存在——Android 6.0已經(jīng)不支持HttpClient" 問題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06Android應(yīng)用程序保持后臺喚醒(使用WakeLock實現(xiàn))
本篇文章主要介紹了使用WakeLock使Android應(yīng)用程序保持后臺喚醒的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點問題
這篇文章主要介紹了基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android實現(xiàn)帶有邊框的ListView和item的方法
這篇文章主要介紹了Android實現(xiàn)帶有邊框的ListView和item的方法,結(jié)合實例形式分析了ListView和item四周添加邊框的實現(xiàn)步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07