android 輕量級資料庫sqlite入門

2021-08-07 12:17:24 字數 1693 閱讀 1006

sqlite 的基本介紹:

一.sqlite資料庫

1.sqlite資料庫的特點

安卓手機自帶, 小巧, 適合在手機中使用

不區分資料型別(主鍵除外)

sql語句和mysql幾乎相同

sqlite不使用jdbc連線, 使用的是android自有的api

每個資料庫對應乙個檔案

二,sqlite 資料庫的建立:

方法 1

1 定義類繼sqliteopenhelper,實現oncreate()方法和onupgrade()方法

2   用sqliteopenhelper的getwritabledatabse()或者getreadabledatabse()的方法來得到乙個sqlitedatabase的例項,然後運算元據sqlite資料庫

方法2使用 sqlitedatabase的openorcreatedatabase(file f,cursor c)方法來例項化乙個物件

但是需要指定乙個路徑

三    對資料庫操作的方法也有兩種以插入為列

方法1contentvalue c=new convalue();

c.put ("id",1);

db.insert("表名",null,c);

由sqllitedatabase提供的insert方法,不需要熟悉sql的語法

方法2db.execsql("insert into dict values(null,?,?)",new string ;

使用sql語法

四execsql()方法可以進行增刪改操作

rawquery()執行查詢操作, 得到cursor, 呼叫movetonext()判斷是否包含資料, 呼叫getstring(), getint()等方法獲取資料

insert(), delete(), update(), query() 四個方法內部也是呼叫execsql()和rawquery()的, 它們在contentprovider中使用更方便

sqlite的基本操作:

sqlite

openhelper

:

package com.example.hncta.sqlite;

import android.content.context;

import android.database.sqlite.sqlitedatabase;

import android.database.sqlite.sqlitedatabase.curso***ctory;

import android.database.sqlite.sqliteopenhelper;

import android.util.log;

class studbhelper extends sqliteopenhelper

// 當第一次建立資料庫的時候,呼叫該方法,在建資料庫的同時建立了資料表

public void oncreate(sqlitedatabase db)

//當更新資料庫的時候執行該方法

public void onupgrade(sqlitedatabase db, int oldversion, int newversion)

}

C 使用sqlite 輕量級資料庫

一,準備工作 3070800.zip 就ok了precompiled binaries for windows sqlite shell win32 x86 3070800.zip 248.28 kib 用於visual studio 專案中引用 二,試用sqlite3.exe 解壓sqlite s...

C 使用sqlite 輕量級資料庫

一 準備工作 precompiled binaries for windows sqlite shell win32 x86 3070800.zip 248.28 kib 用於visual studio 專案中引用 二,試用sqlite3.exe 解壓sqlite shell win32 x86 3...

利用 SQLAlchemy 實現輕量級資料庫遷移

本文介紹結合 python 的幾個工具,實現輕量級的資料庫遷移。在日常工作中,可能需要將資料遷移到不同的資料庫,可以考慮下面一些簡單的辦法 假設我們現在需要把 sql server 中 emp master 表的資料遷移到 sqlite 資料庫的同名表中。遷移的過程分為兩步 首先利用 sqlacod...