Android Studio 中使用 SQLite 數據庫開發完整指南(Kotlin版本)

在這里插入圖片描述

文章目錄

    • 1. 項目準備
      • 1.1 創建新項目
      • 1.2 添加必要依賴
    • 2. 數據庫設計
    • 3. 實現數據庫
      • 3.1 創建實體類 (Entity)
      • 3.2 創建數據訪問對象 (DAO)
      • 3.3 創建數據庫類
    • 4. 創建 Repository
    • 5. 創建 ViewModel
    • 6. 實現 UI 層
      • 6.1 創建筆記列表 Activity
        • activity_notes_list.xml
        • NotesListActivity.kt
      • 6.2 創建筆記詳情 Activity
        • activity_note_detail.xml
        • NoteDetailActivity.kt
      • 6.3 創建 RecyclerView Adapter
      • 6.4 創建 Application 類
    • 7. 添加菜單資源
    • 8. 添加字符串資源
    • 9. 添加圖標資源
    • 10. 運行和測試應用
    • 11. 數據庫調試技巧
      • 11.1 查看數據庫內容
      • 11.2 使用 Stetho 進行調試
    • 12. 數據庫遷移
      • 12.1 修改實體類
      • 12.2 更新數據庫版本
      • 12.3 添加遷移策略
    • 13. 性能優化建議
    • 14. 完整項目結構
    • 15. 總結

在這里插入圖片描述

1. 項目準備

1.1 創建新項目

  1. 打開 Android Studio
  2. 選擇 “Start a new Android Studio project”
  3. 選擇 “Empty Activity” 模板
  4. 設置項目名稱(例如 “SQLiteDemo”)
  5. 選擇語言(Kotlin 或 Java,本教程以 Kotlin 為例)
  6. 設置最低 API 級別(建議 API 21 或更高)
  7. 點擊 “Finish” 完成項目創建

1.2 添加必要依賴

確保 build.gradle (Module: app) 中包含以下依賴:

dependencies {implementation 'androidx.core:core-ktx:1.7.0'implementation 'androidx.appcompat:appcompat:1.4.1'implementation 'com.google.android.material:material:1.5.0'implementation 'androidx.constraintlayout:constraintlayout:2.1.3'// Room 數據庫(SQLite 的抽象層)implementation "androidx.room:room-runtime:2.4.2"implementation "androidx.room:room-ktx:2.4.2"kapt "androidx.room:room-compiler:2.4.2"// 協程支持implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'// ViewModel 和 LiveDataimplementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.3'androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

點擊 “Sync Now” 同步項目。

2. 數據庫設計

假設我們要創建一個簡單的筆記應用,包含以下數據表:

  • notes 表:
    • id: 主鍵,自增
    • title: 筆記標題
    • content: 筆記內容
    • created_at: 創建時間
    • updated_at: 更新時間

3. 實現數據庫

3.1 創建實體類 (Entity)

com.yourpackage.model 包下創建 Note.kt 文件:

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.*@Entity(tableName = "notes")
data class Note(@PrimaryKey(autoGenerate = true)val id: Long = 0,var title: String,var content: String,val created_at: Date = Date(),var updated_at: Date = Date()
)

3.2 創建數據訪問對象 (DAO)

com.yourpackage.dao 包下創建 NoteDao.kt 文件:

import androidx.lifecycle.LiveData
import androidx.room.*
import com.yourpackage.model.Note@Dao
interface NoteDao {@Insert(onConflict = OnConflictStrategy.REPLACE)suspend fun insertNote(note: Note): Long@Updatesuspend fun updateNote(note: Note)@Deletesuspend fun deleteNote(note: Note)@Query("SELECT * FROM notes ORDER BY updated_at DESC")fun getAllNotes(): LiveData<List<Note>>@Query("SELECT * FROM notes WHERE id = :noteId")suspend fun getNoteById(noteId: Long): Note?@Query("SELECT * FROM notes WHERE title LIKE :query OR content LIKE :query ORDER BY updated_at DESC")fun searchNotes(query: String): LiveData<List<Note>>
}

3.3 創建數據庫類

com.yourpackage.database 包下創建 AppDatabase.kt 文件:

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.yourpackage.dao.NoteDao
import com.yourpackage.model.Note@Database(entities = [Note::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {abstract fun noteDao(): NoteDaocompanion object {@Volatileprivate var INSTANCE: AppDatabase? = nullfun getDatabase(context: Context): AppDatabase {return INSTANCE ?: synchronized(this) {val instance = Room.databaseBuilder(context.applicationContext,AppDatabase::class.java,"notes_database").fallbackToDestructiveMigration() // 數據庫升級策略,簡單應用可以這樣設置.build()INSTANCE = instanceinstance}}}
}

4. 創建 Repository

com.yourpackage.repository 包下創建 NoteRepository.kt 文件:

import androidx.lifecycle.LiveData
import com.yourpackage.dao.NoteDao
import com.yourpackage.model.Note
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContextclass NoteRepository(private val noteDao: NoteDao) {val allNotes: LiveData<List<Note>> = noteDao.getAllNotes()suspend fun insert(note: Note): Long {return withContext(Dispatchers.IO) {noteDao.insertNote(note)}}suspend fun update(note: Note) {withContext(Dispatchers.IO) {note.updated_at = Date()noteDao.updateNote(note)}}suspend fun delete(note: Note) {withContext(Dispatchers.IO) {noteDao.deleteNote(note)}}suspend fun getNoteById(id: Long): Note? {return withContext(Dispatchers.IO) {noteDao.getNoteById(id)}}fun searchNotes(query: String): LiveData<List<Note>> {return noteDao.searchNotes("%$query%")}
}

5. 創建 ViewModel

com.yourpackage.viewmodel 包下創建 NoteViewModel.kt 文件:

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.yourpackage.model.Note
import com.yourpackage.repository.NoteRepository
import kotlinx.coroutines.launchclass NoteViewModel(private val repository: NoteRepository) : ViewModel() {val allNotes = repository.allNotesfun insert(note: Note) = viewModelScope.launch {repository.insert(note)}fun update(note: Note) = viewModelScope.launch {repository.update(note)}fun delete(note: Note) = viewModelScope.launch {repository.delete(note)}fun getNoteById(id: Long) = viewModelScope.launch {repository.getNoteById(id)}fun searchNotes(query: String) = repository.searchNotes(query).asLiveData()
}class NoteViewModelFactory(private val repository: NoteRepository) : ViewModelProvider.Factory {override fun <T : ViewModel> create(modelClass: Class<T>): T {if (modelClass.isAssignableFrom(NoteViewModel::class.java)) {@Suppress("UNCHECKED_CAST")return NoteViewModel(repository) as T}throw IllegalArgumentException("Unknown ViewModel class")}
}

6. 實現 UI 層

6.1 創建筆記列表 Activity

創建 NotesListActivity.kt 和對應的布局文件 activity_notes_list.xml

activity_notes_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ui.NotesListActivity"><com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/Theme.SQLiteDemo.AppBarOverlay"><androidx.appcompat.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/Theme.SQLiteDemo.PopupOverlay"app:title="@string/app_name" /><com.google.android.material.textfield.TextInputLayoutandroid:id="@+id/search_layout"android:layout_width="match_parent"android:layout_height="wrap_content"style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"><com.google.android.material.textfield.TextInputEditTextandroid:id="@+id/search_input"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="@string/search_hint"android:imeOptions="actionSearch"android:inputType="text" /></com.google.android.material.textfield.TextInputLayout></com.google.android.material.appbar.AppBarLayout><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/notes_recycler_view"android:layout_width="match_parent"android:layout_height="match_parent"android:clipToPadding="false"android:paddingBottom="72dp"app:layout_behavior="@string/appbar_scrolling_view_behavior" /><com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/fab_add_note"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|end"android:layout_margin="16dp"android:contentDescription="@string/add_note"android:src="@drawable/ic_add"app:backgroundTint="@color/purple_500"app:tint="@android:color/white" /></androidx.coordinatorlayout.widget.CoordinatorLayout>
NotesListActivity.kt
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.yourpackage.R
import com.yourpackage.adapter.NotesAdapter
import com.yourpackage.databinding.ActivityNotesListBinding
import com.yourpackage.model.Note
import com.yourpackage.viewmodel.NoteViewModel
import com.yourpackage.viewmodel.NoteViewModelFactoryclass NotesListActivity : AppCompatActivity() {private lateinit var binding: ActivityNotesListBindingprivate lateinit var notesAdapter: NotesAdapterprivate val viewModel: NoteViewModel by viewModels {NoteViewModelFactory((application as NotesApplication).repository)}override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ActivityNotesListBinding.inflate(layoutInflater)setContentView(binding.root)setSupportActionBar(binding.toolbar)setupRecyclerView()setupSearch()setupFAB()observeNotes()}private fun setupRecyclerView() {notesAdapter = NotesAdapter { note ->// 點擊筆記項時的操作val intent = Intent(this, NoteDetailActivity::class.java).apply {putExtra(NoteDetailActivity.EXTRA_NOTE_ID, note.id)}startActivity(intent)}binding.notesRecyclerView.apply {layoutManager = LinearLayoutManager(this@NotesListActivity)adapter = notesAdaptersetHasFixedSize(true)}}private fun setupSearch() {binding.searchInput.setOnEditorActionListener { _, actionId, _ ->if (actionId == EditorInfo.IME_ACTION_SEARCH) {val query = binding.searchInput.text.toString().trim()if (query.isNotEmpty()) {viewModel.searchNotes(query).observe(this) { notes ->notesAdapter.submitList(notes)}} else {observeNotes() // 如果查詢為空,返回所有筆記}true} else {false}}}private fun setupFAB() {binding.fabAddNote.setOnClickListener {val intent = Intent(this, NoteDetailActivity::class.java)startActivity(intent)}}private fun observeNotes() {viewModel.allNotes.observe(this) { notes ->notesAdapter.submitList(notes)}}override fun onCreateOptionsMenu(menu: Menu): Boolean {menuInflater.inflate(R.menu.menu_main, menu)return true}override fun onOptionsItemSelected(item: MenuItem): Boolean {return when (item.itemId) {R.id.action_delete_all -> {deleteAllNotes()true}else -> super.onOptionsItemSelected(item)}}private fun deleteAllNotes() {viewModel.allNotes.value?.let { notes ->if (notes.isNotEmpty()) {for (note in notes) {viewModel.delete(note)}Snackbar.make(binding.root, "All notes deleted", Snackbar.LENGTH_SHORT).show()}}}
}

6.2 創建筆記詳情 Activity

創建 NoteDetailActivity.kt 和對應的布局文件 activity_note_detail.xml

activity_note_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ui.NoteDetailActivity"><com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/Theme.SQLiteDemo.AppBarOverlay"><androidx.appcompat.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/Theme.SQLiteDemo.PopupOverlay" /></com.google.android.material.appbar.AppBarLayout><androidx.core.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="16dp"><com.google.android.material.textfield.TextInputLayoutandroid:id="@+id/title_layout"android:layout_width="match_parent"android:layout_height="wrap_content"style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"><com.google.android.material.textfield.TextInputEditTextandroid:id="@+id/title_input"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="@string/title_hint"android:inputType="textCapSentences|textAutoCorrect"android:maxLines="1" /></com.google.android.material.textfield.TextInputLayout><com.google.android.material.textfield.TextInputLayoutandroid:id="@+id/content_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="16dp"style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"><com.google.android.material.textfield.TextInputEditTextandroid:id="@+id/content_input"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="@string/content_hint"android:inputType="textMultiLine|textCapSentences|textAutoCorrect"android:minLines="5"android:gravity="top" /></com.google.android.material.textfield.TextInputLayout></LinearLayout></androidx.core.widget.NestedScrollView><com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/fab_save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|end"android:layout_margin="16dp"android:contentDescription="@string/save_note"android:src="@drawable/ic_save"app:backgroundTint="@color/purple_500"app:tint="@android:color/white" /></androidx.coordinatorlayout.widget.CoordinatorLayout>
NoteDetailActivity.kt
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import com.yourpackage.R
import com.yourpackage.databinding.ActivityNoteDetailBinding
import com.yourpackage.model.Note
import com.yourpackage.viewmodel.NoteViewModel
import com.yourpackage.viewmodel.NoteViewModelFactory
import java.util.*class NoteDetailActivity : AppCompatActivity() {companion object {const val EXTRA_NOTE_ID = "extra_note_id"}private lateinit var binding: ActivityNoteDetailBindingprivate val viewModel: NoteViewModel by viewModels {NoteViewModelFactory((application as NotesApplication).repository)}private var noteId: Long = -1Lprivate var isNewNote = trueoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ActivityNoteDetailBinding.inflate(layoutInflater)setContentView(binding.root)setSupportActionBar(binding.toolbar)supportActionBar?.setDisplayHomeAsUpEnabled(true)noteId = intent.getLongExtra(EXTRA_NOTE_ID, -1L)isNewNote = noteId == -1Lif (!isNewNote) {loadNote()}setupSaveButton()setupTextWatchers()}private fun loadNote() {viewModel.getNoteById(noteId)viewModel.allNotes.observe(this) { notes ->notes.find { it.id == noteId }?.let { note ->binding.titleInput.setText(note.title)binding.contentInput.setText(note.content)}}}private fun setupSaveButton() {binding.fabSave.setOnClickListener {saveNote()}}private fun setupTextWatchers() {binding.titleInput.addTextChangedListener(object : TextWatcher {override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}override fun afterTextChanged(s: Editable?) {validateInputs()}})binding.contentInput.addTextChangedListener(object : TextWatcher {override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}override fun afterTextChanged(s: Editable?) {validateInputs()}})}private fun validateInputs(): Boolean {val titleValid = binding.titleInput.text?.isNotBlank() ?: falseval contentValid = binding.contentInput.text?.isNotBlank() ?: falsebinding.titleLayout.error = if (!titleValid) getString(R.string.title_required) else nullbinding.contentLayout.error = if (!contentValid) getString(R.string.content_required) else nullreturn titleValid && contentValid}private fun saveNote() {if (!validateInputs()) returnval title = binding.titleInput.text.toString()val content = binding.contentInput.text.toString()if (isNewNote) {val note = Note(title = title, content = content)viewModel.insert(note)Snackbar.make(binding.root, "Note saved", Snackbar.LENGTH_SHORT).show()finish()} else {viewModel.allNotes.value?.find { it.id == noteId }?.let { existingNote ->val updatedNote = existingNote.copy(title = title,content = content,updated_at = Date())viewModel.update(updatedNote)Snackbar.make(binding.root, "Note updated", Snackbar.LENGTH_SHORT).show()finish()}}}override fun onOptionsItemSelected(item: MenuItem): Boolean {return when (item.itemId) {android.R.id.home -> {onBackPressed()true}else -> super.onOptionsItemSelected(item)}}
}

6.3 創建 RecyclerView Adapter

com.yourpackage.adapter 包下創建 NotesAdapter.kt 文件:

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.yourpackage.R
import com.yourpackage.databinding.ItemNoteBinding
import com.yourpackage.model.Note
import java.text.SimpleDateFormat
import java.util.*class NotesAdapter(private val onItemClick: (Note) -> Unit) :ListAdapter<Note, NotesAdapter.NoteViewHolder>(NoteDiffCallback()) {override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {val binding = ItemNoteBinding.inflate(LayoutInflater.from(parent.context),parent,false)return NoteViewHolder(binding, onItemClick)}override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {holder.bind(getItem(position))}class NoteViewHolder(private val binding: ItemNoteBinding,private val onItemClick: (Note) -> Unit) : RecyclerView.ViewHolder(binding.root) {fun bind(note: Note) {binding.apply {noteTitle.text = note.titlenoteContent.text = note.contentval dateFormat = SimpleDateFormat("MMM dd, yyyy - hh:mm a", Locale.getDefault())noteDate.text = dateFormat.format(note.updated_at)root.setOnClickListener {onItemClick(note)}}}}private class NoteDiffCallback : DiffUtil.ItemCallback<Note>() {override fun areItemsTheSame(oldItem: Note, newItem: Note): Boolean {return oldItem.id == newItem.id}override fun areContentsTheSame(oldItem: Note, newItem: Note): Boolean {return oldItem == newItem}}
}

創建對應的列表項布局文件 item_note.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="8dp"app:cardCornerRadius="8dp"app:cardElevation="4dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="16dp"><TextViewandroid:id="@+id/note_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:textAppearance="@style/TextAppearance.AppCompat.Headline"android:textColor="@android:color/black" /><TextViewandroid:id="@+id/note_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:ellipsize="end"android:maxLines="2"android:textAppearance="@style/TextAppearance.AppCompat.Body1"android:textColor="@android:color/darker_gray" /><TextViewandroid:id="@+id/note_date"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:textAppearance="@style/TextAppearance.AppCompat.Caption"android:textColor="@android:color/darker_gray" /></LinearLayout>
</com.google.android.material.card.MaterialCardView>

6.4 創建 Application 類

com.yourpackage 包下創建 NotesApplication.kt 文件:

import android.app.Application
import com.yourpackage.database.AppDatabase
import com.yourpackage.repository.NoteRepositoryclass NotesApplication : Application() {val database by lazy { AppDatabase.getDatabase(this) }val repository by lazy { NoteRepository(database.noteDao()) }
}

更新 AndroidManifest.xml 文件,添加 android:name 屬性:

<applicationandroid:name=".NotesApplication"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.SQLiteDemo"><!-- 其他配置 -->
</application>

7. 添加菜單資源

res/menu 目錄下創建 menu_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><itemandroid:id="@+id/action_delete_all"android:icon="@drawable/ic_delete"android:title="@string/delete_all"app:showAsAction="never" />
</menu>

8. 添加字符串資源

res/values/strings.xml 文件中添加以下字符串:

<resources><string name="app_name">SQLite Notes</string><string name="title_hint">Title</string><string name="content_hint">Content</string><string name="search_hint">Search notes...</string><string name="add_note">Add new note</string><string name="save_note">Save note</string><string name="delete_all">Delete all notes</string><string name="title_required">Title is required</string><string name="content_required">Content is required</string>
</resources>

9. 添加圖標資源

確保在 res/drawable 目錄下有以下矢量圖標:

  • ic_add.xml (添加按鈕圖標)
  • ic_save.xml (保存按鈕圖標)
  • ic_delete.xml (刪除按鈕圖標)

10. 運行和測試應用

現在,您可以運行應用程序并測試以下功能:

  1. 添加新筆記
  2. 查看筆記列表
  3. 編輯現有筆記
  4. 刪除筆記
  5. 搜索筆記
  6. 刪除所有筆記

11. 數據庫調試技巧

11.1 查看數據庫內容

  1. 在 Android Studio 中打開 “Device File Explorer” (View -> Tool Windows -> Device File Explorer)
  2. 導航到 /data/data/com.yourpackage/databases/
  3. 找到 notes_database 文件
  4. 右鍵點擊并選擇 “Save As” 將其保存到本地
  5. 使用 SQLite 瀏覽器工具(如 DB Browser for SQLite)打開該文件查看內容

11.2 使用 Stetho 進行調試

添加 Stetho 依賴到 build.gradle:

implementation 'com.facebook.stetho:stetho:1.6.0'

NotesApplication.kt 中初始化 Stetho:

import com.facebook.stetho.Stethoclass NotesApplication : Application() {override fun onCreate() {super.onCreate()Stetho.initializeWithDefaults(this)}// 其他代碼...
}

運行應用后,在 Chrome 瀏覽器中訪問 chrome://inspect 可以查看和調試數據庫。

12. 數據庫遷移

當您需要更改數據庫結構時(例如添加新表或修改現有表),需要進行數據庫遷移。

12.1 修改實體類

例如,我們要為 Note 添加一個 is_pinned 字段:

@Entity(tableName = "notes")
data class Note(// 現有字段...var is_pinned: Boolean = false
)

12.2 更新數據庫版本

修改 AppDatabase.kt:

@Database(entities = [Note::class], version = 2, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {// ...
}

12.3 添加遷移策略

val migration1to2 = object : Migration(1, 2) {override fun migrate(database: SupportSQLiteDatabase) {database.execSQL("ALTER TABLE notes ADD COLUMN is_pinned INTEGER NOT NULL DEFAULT 0")}
}// 在 databaseBuilder 中添加遷移
val instance = Room.databaseBuilder(context.applicationContext,AppDatabase::class.java,"notes_database"
).addMigrations(migration1to2).build()

13. 性能優化建議

  1. 使用事務:對于批量操作,使用事務可以顯著提高性能:
@Dao
interface NoteDao {@Transactionsuspend fun insertAll(notes: List<Note>) {notes.forEach { insertNote(it) }}
}
  1. 索引優化:為常用查詢字段添加索引:
@Entity(tableName = "notes", indices = [Index(value = ["title"], unique = false)])
data class Note(// ...
)
  1. 分頁加載:對于大量數據,使用 Paging 庫:
@Query("SELECT * FROM notes ORDER BY updated_at DESC")
fun getPagedNotes(): PagingSource<Int, Note>
  1. 避免在主線程操作數據庫:始終確保數據庫操作在后臺線程執行。

14. 完整項目結構

最終項目結構應類似于:

com.yourpackage
├── adapter
│   └── NotesAdapter.kt
├── dao
│   └── NoteDao.kt
├── database
│   └── AppDatabase.kt
├── model
│   └── Note.kt
├── repository
│   └── NoteRepository.kt
├── ui
│   ├── NotesListActivity.kt
│   └── NoteDetailActivity.kt
├── viewmodel
│   ├── NoteViewModel.kt
│   └── NoteViewModelFactory.kt
└── NotesApplication.kt

15. 總結

本指南詳細介紹了在 Android Studio 中使用 SQLite 數據庫的完整開發流程,包括:

  1. 設置項目和依賴
  2. 設計數據庫結構
  3. 實現 Room 數據庫組件(Entity, DAO, Database)
  4. 創建 Repository 層
  5. 實現 ViewModel
  6. 構建用戶界面
  7. 添加數據庫遷移支持
  8. 性能優化建議

通過遵循這些步驟,您可以構建一個功能完善、結構清晰的 Android 應用,充分利用 SQLite 數據庫的強大功能。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/77577.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/77577.shtml
英文地址,請注明出處:http://en.pswp.cn/web/77577.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Vue基礎(7)_計算屬性

計算屬性(computed) 一、使用方式&#xff1a; 1.定義計算屬性&#xff1a; 在Vue組件中&#xff0c;通過在 computed 對象中定義計算屬性名稱及對應的計算函數來創建計算屬性。計算函數會返回計算屬性的值。 2.在模板中使用計算屬性&#xff1a; 在Vue的模板中&#xff0c;您…

辛格迪客戶案例 | 華道生物細胞治療生產及追溯項目(CGTS)

01 華道&#xff08;上海&#xff09;生物醫藥有限公司&#xff1a;細胞治療領域的創新先鋒 華道&#xff08;上海&#xff09;生物醫藥有限公司&#xff08;以下簡稱“華道生物”&#xff09;是一家專注于細胞治療技術研發與應用的創新型企業&#xff0c;尤其在CAR-T細胞免疫…

[26] cuda 應用之 nppi 實現圖像格式轉換

[26] cuda 應用之 nppi 實現圖像格式轉換 講述 nppi 接口定義通過nppi實現 bayer 格式轉rgb格式官網參考信息:http://gwmodel.whu.edu.cn/docs/CUDA/npp/group__image__color__debayer.html#details1. 接口定義 官網關于轉換的原理是這么寫的: Grayscale Color Filter Array …

2025“釘耙編程”中國大學生算法設計春季聯賽(8)10031007

題目的意思很好理解找從最左邊到最右邊最短路&#xff08;BFS&#xff09; #include <bits/stdc.h> using namespace std; int a[510][510]; // 存儲網格中每個位置是否有障礙&#xff08;1表示有障礙&#xff0c;0表示無障礙&#xff09; int v[510][510]; // 記錄每…

【Linux】第十一章 管理網絡

目錄 1.TCP/IP網絡模型 物理層&#xff08;Physical&#xff09; 數據鏈路層&#xff08;Date Link&#xff09; 網絡層&#xff08;Internet&#xff09; 傳輸層&#xff08;Transport&#xff09; 應用層&#xff08;Application&#xff09; 2. 對于 IPv4 地址&#…

python_股票月數據趨勢判斷

目錄 前置 代碼 視頻&月數據 前置 1 A股月數據趨勢大致判斷&#xff0c;做一個粗略的篩選 2 邏輯&#xff1a; 1&#xff09;取最近一次歷史最高點 2&#xff09;以1&#xff09;中最高點為分界點&#xff0c;只看右側數據&#xff0c;取最近一次最低點 3&#xf…

Python PyAutoGUI庫【GUI 自動化庫】深度解析與實戰指南

一、核心工作原理 底層驅動機制&#xff1a; 通過操作系統原生API模擬輸入使用ctypes庫調用Windows API/Mac Cocoa/Xlib屏幕操作依賴Pillow庫進行圖像處理 事件模擬流程&#xff1a; #mermaid-svg-1CGDRNzFNEffhvSa {font-family:"trebuchet ms",verdana,arial,sans…

Spring框架allow-bean-definition-overriding詳細解釋

Spring框架中&#xff0c;allow-bean-definition-overriding 是一個控制是否允許覆蓋同名Bean定義的配置屬性。以下是詳細說明&#xff1a; ?1. 作用? ?允許/禁止Bean定義覆蓋?&#xff1a;當Spring容器中檢測到多個同名的Bean定義時&#xff0c;此配置決定是否允許后續的…

機器人抓取位姿檢測——GRCN訓練及測試教程(Pytorch)

機器人抓取位姿檢測——GRCN訓練及測試教程(Pytorch) 這篇文章主要介紹了2020年IROS提出的一種名為GRCN的檢測模型,給出了代碼各部分的說明,并給出windows系統下可以直接復現的完整代碼,包含Cornell數據集。 模型結構圖 github源碼地址:https://github.com/skumra/robo…

在web應用后端接入內容審核——以騰訊云音頻審核為例(Go語言示例)

騰訊云對象存儲數據萬象&#xff08;Cloud Infinite&#xff0c;CI&#xff09;為用戶提供圖片、視頻、語音、文本等文件的內容安全智能審核服務&#xff0c;幫助用戶有效識別涉黃、違法違規和廣告審核&#xff0c;規避運營風險。本文以音頻審核為例給出go語言示例代碼與相應結…

GraphRAG知識庫概要設計展望

最近研究了一下GraphRAG&#xff0c;寫了一個文檔轉換工具還有圖可視化工具&#xff0c;結合langchain構建RAG經驗&#xff0c;還有以前的數據平臺&#xff0c;做了一個知識庫概要設計&#xff0c;具體應用歡迎留言探討。 一、GraphRAG整體概述 GraphRAG圖基檢索增強生成&…

Android Studio 日志系統詳解

文章目錄 一、Android 日志系統基礎1. Log 類2. 日志級別 二、Android Studio 中的 Logcat1. 打開 Logcat2. Logcat 界面組成3. 常用 Logcat 命令 三、高級日志技巧1. 自定義日志工具類2. 打印方法調用棧3. 打印長日志4. JSON 和 XML 格式化輸出 四、Logcat 高級功能1. 自定義日…

深度對比:Objective-C與Swift的RunTime機制與底層原理

1. RunTime簡介 RunTime&#xff08;運行時&#xff09;是指程序在運行過程中動態管理類型、對象、方法等的機制。Objective-C 和 Swift 都擁有自己的運行時系統&#xff0c;但設計理念和實現方式有很大不同。理解 RunTime 的底層原理&#xff0c;是掌握 iOS 高級開發的關鍵。…

使用手機錄制rosbag包

文章目錄 簡介錄制工具錄制步驟錄制設置設置IMU錄制頻率設置相機分辨率拍照模式錄制模式數據制作獲取數據數據轉為rosbag查看rosbag簡介 ROS數據包(rosbag)是ROS系統中用于記錄和回放傳感器數據的重要工具,通常用于算法調試、系統測試和數據采集。傳統上,rosbag依賴于ROS環…

淺談PCB傳輸線(一)

前言&#xff1a;淺談傳輸線的類型&#xff0c;以及傳輸線的一些行為特性。 1.傳輸線的種類 2.互連線被視為傳輸線的場景 3.傳輸線的行為特性*** 1.傳輸線的種類 PCB 中的信號傳輸線通常有兩種基本類型: 微帶線和帶狀線。此外&#xff0c;還有第三種類型–共面線(沒有參考平面…

【angular19】入門基礎教程(一):項目的搭建與啟動

angular現在發展的越來越能完善了&#xff0c;在vue和react的強勢競爭下&#xff0c;它迎來了自己的巨大變革。項目工程化越來越好&#xff0c;也開始擁抱了vite這種高效的構建方式。所以&#xff0c;我們有必要來學習這么一個框架了。 項目實現效果 nodejs環境 Node.js - v^…

在前端應用領域驅動設計(DDD):必要性、挑戰與實踐指南

引言 領域驅動設計&#xff08;Domain-Driven Design&#xff0c;簡稱 DDD&#xff09;起源于后端復雜業務系統建模領域&#xff0c;是 Eric Evans 在 2003 年提出的一套理論體系。近年來&#xff0c;隨著前端工程化與業務復雜度的持續提升&#xff0c;"前端也要 DDD&quo…

一文了解 模型上下文協議(MCP)

MCP&#xff08;Model Context Protocol&#xff0c;模型上下文協議&#xff09;是由Anthropic公司于2024年11月推出的一項開放標準協議&#xff0c;旨在解決大型語言模型&#xff08;LLM&#xff09;與外部數據源和工具之間的通信問題。其核心目標是通過提供一個標準化的接口&…

面向全球的行業開源情報體系建設方法論——以易海聚實戰經驗為例

在全球數字化轉型加速的背景下&#xff0c;如何精準鎖定目標領域的關鍵信息源&#xff0c;構建可持續迭代的情報網絡&#xff0c;已成為企業戰略決策的核心能力。深圳易海聚信息技術有限公司&#xff08;以下簡稱“易海聚”&#xff09;深耕開源情報領域十余年&#xff0c;其自…

UDP協議詳解+代碼演示

1、UDP協議基礎 1. UDP是什么&#xff1f; UDP&#xff08;User Datagram Protocol&#xff0c;用戶數據報協議&#xff09;是傳輸層的核心協議之一&#xff0c;與TCP并列。它的主要特點是&#xff1a;???? 無連接&#xff1a;通信前不需要建立連接&#xff08;知道對端的…