A. android上如何使用sqlite資料庫
SQLite 一個非常流行的嵌入式資料庫,它支持 SQL 語言,並且只利用很少的內存就有很好的性能。此外它還是開源的,任何人都可以使用它。許多開源項目((Mozilla, PHP, Python)都使用了 SQLite.
Android 開發中使用 SQLite 資料庫
Activites 可以通過 Content Provider 或者 Service 訪問一個資料庫。下面會詳細講解如果創建資料庫,添加數據和查詢資料庫。
創建資料庫
Android 不自動提供資料庫。在 Android 應用程序中使用 SQLite,必須自己創建資料庫,然後創建表、索引,填充數據。Android 提供了 SQLiteOpenHelper 幫助你創建一個資料庫,你只要繼承 SQLiteOpenHelper 類,就可以輕松的創建資料庫。SQLiteOpenHelper 類根據開發應用程序的需要,封裝了創建和更新資料庫使用的邏輯。SQLiteOpenHelper 的子類,至少需要實現三個方法:
構造函數,調用父類 SQLiteOpenHelper 的構造函數。這個方法需要四個參數:上下文環境(例如,一個 Activity),資料庫名字,一個可選的游標工廠(通常是 Null),一個代表你正在使用的資料庫模型版本的整數。
onCreate()方法,它需要一個 SQLiteDatabase 對象作為參數,根據需要對這個對象填充表和初始化數據。
onUpgrage() 方法,它需要三個參數,一個 SQLiteDatabase 對象,一個舊的版本號和一個新的版本號,這樣你就可以清楚如何把一個資料庫從舊的模型轉變到新的模型。
B. android stio用sqlite怎麼創建資料庫
1.使用intelij idea創建一個andorid項目
2.創建如下工具類:
MyDBHelper.java
package com.amos.android_database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by amosli on 14-6-10.
*/
public class MyDBHelper extends SQLiteOpenHelper{
/**
* 創建資料庫的構造方法
* @param context 應用程序上下文
* name 資料庫的名字
* factory 查詢資料庫的游標工廠一般情況下用sdk默認的
* version 資料庫的版本一般大於0
*/
public MyDBHelper(Context context) {
super(context, "test.db", null, 4);
}
private String tag = "MyDBHelper.class";
/**
* 在資料庫第一次創建時會執行
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(tag,"onCreate.....");
//創建一個資料庫
db.execSQL("create table person (personid integer primary key autoincrement ,name varchar(30) )");
}
/**
* 更新數據的時候調用的方法
* @param db
* @param oldVersion
* @param newVersion
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(tag,"onUpgrade*******");
//增加一列
db.execSQL("alter table person add phone varchar(13) null");
}
}
MyActivity.java
package com.amos.android_database;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyDBHelper myDBHelper = new MyDBHelper(this);
myDBHelper.getReadableDatabase();
myDBHelper.close();
}
}
3.打開生成的test.db
1).下載安裝SQLiteStudio(SQLite的可視化工具)
下載地址:http://www.sqlitestudio.pl/
安裝:
chmod 777 sqlitestudio-2.1.5.bin
./sqlitestudio-2.1.5.bin
執行上面的linux命令,第一句是賦許可權,第二句是打開工具
2)從avd中導出test.db
打開DDMS,從data/data/com.amos.andriod_database/databases下導出test.db
3).用sqlitestudio打開test.db
添加資料庫
選擇test.db
執行onCreate方法時創建的person表
執行onUpgrade方法更新語句的時候
C. android怎麼在手機SD卡上創建SQLite資料庫
在sd卡上創建資料庫
通過android的sqliteopenhelper類的源碼,可以看到sqliteopenhelper類的getwritabledatabase
這個介面實際上調用的是context的openorcreatedatabase方法,而這個方法是不支持帶路徑的資料庫名
稱的,也就是說,用這個方法創建的資料庫只能放在/data/data/包名稱/
目錄下;要想在sd卡上創建資料庫
,我們可以調用sqlitedatabase類的openorcreatedatabase方法,這個方法是支持帶路徑的資料庫名稱的。
D. 如何在android自帶的sqlite3中創建一個資料庫文件,請大蝦門詳細指導
Android中提供4種數據存儲的方法:(1)SharedPreferences,用鍵值對的方式來存儲數據,是一種輕量級的存儲機制,可以存儲一些屬性等。(2)Files:文件輸入輸出流的方式存儲數據,FileInputStream和FileOutputStream。在Android中,文件是一個應用程序私有的,一個應用程序無法讀寫其他應用程序的文件。寫入SD卡除外。(3)SQLite(4)網路(這個不算吧~~~--!)。
SQLite:
(1)創建和打開資料庫可以使用方法openOrCreateDatabase,它會自動去檢測是否存在資料庫,如果存在則打開,如果不存在則創建一個資料庫;成功返回一個SQliteDatabase對象。(2)創建一張表通過SQL語句實現,調用sqliteDatabase對象的execSQL方法,執行創建表的SQL語句。
(3)向數據表中添加一條記錄可以直接通過SQL語句實現,也可以使用ContentValue對象,ContentValue對象是一個Map,Key是欄位名,Value是值。Cv.put(key,value);然後調用sqliteDatabase對象的
insert(tableName,null,cv)方法插入數據。
(4)刪除數據可以直接執行SQL,也可以執行sqliteDatabase的delete方法。
(5)同理修改數據也是執行SQL或調用update方法,需要傳入ContenValue的對象表示修改的內容。
(6)關閉資料庫sqliteDatabase.close();
(7)刪除指定表調用SQL語句即可。
(8)查詢:在Android中查詢數據是通過Cursor類來實現的,當我們使用SQLiteDatabase.query()方法時,會得到一個Cursor對象,Cursor對象指向的是每條數據。例如cur.moveToFirst();cur.moveToNext();等。在實際開發中,為了能夠更好地管理和維護資料庫,我們會封裝一個繼承自SQLiteOpenHelper類的資料庫操作類。SQLiteOpenHelper的構造方法中分別需要傳入Context、資料庫名稱、CursorFactory(一般默認null)、資料庫版本號。在SQLiteOpenHelper中首先執行的是onCreate方法(當資料庫第一次被創建時)。在構造函數中並沒有真正創建資料庫,而是調用getWriteableDatabase或者getReadableDatabase方法時才真正去創建資料庫,並且返回一個SQLiteDatabase對象。
E. 如何往android中添加資料庫
一、新建外部SQLite資料庫
(1)下載並安裝 SQLite可視化管理工具(SQLite Expert Pro) v3.4.17 破解版
http://www.cr173.com/soft/36343.html
(2)將你手頭上的數據放到EXCEL表格中,保存為CSV格式的數據
(3)在此工具中按照你現有的數據格式新建資料庫和表,如資料庫為:contact.db,表為employee
(4)通過此工具菜單欄中Import/Export下的Import text file(CSV,TSC)功能,將你現有的CSV數據導入到你新建的數據表中(主要目的是省的一個一個的錄入了)
二、在eclipse中新建一個android app工程,並在新建的工程文件夾點右鍵new->folder,在res文件夾下新建raw文件夾(如果有就不用新建了)
三、用滑鼠將新建的SQLite資料庫文件contact.db拖動到新建工程的res下的raw文件下,出現提示,選擇
四、程序代碼
private static final String DATABASE_PATH = "/data/data/你的主程序包路徑(如:com.szair.contact)/databases";
private static final int DATABASE_VERSION = 0;
private static final String DATABASE_NAME = "contact.db";
private static String outFileName = DATABASE_PATH + "/" + DATABASE_NAME;
try {
buildDatabase();//見下
} catch (Exception e) {
e.printStackTrace();
}
//SQLiteDatabase對象
SQLiteDatabase db=SQLiteDatabase.openDatabase(outFileName, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
String t="SELECT 欄位名1,欄位名2 FROM employee WHERE **** ORDER BY ***";
Cursor c =db.rawQuery(t, null);
if(c.moveToFirst()){
for(int i=0;i
{
String zian1=c.getString(0);//欄位1的數據
String zian2=c.getString(1);//欄位1的數據
}
}
------------------------------------------------
//前面用到的buildDatabase方法
private void buildDatabase() throws Exception{
InputStream myInput = getResources().openRawResource(R.raw.sz_contact);
File file = new File(outFileName);
File dir = new File(DATABASE_PATH);
if (!dir.exists()) {
if (!dir.mkdir()) {
throw new Exception("創建失敗");
}
}
if (!file.exists()) {
try {
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.close();
myInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
五、程序發布
按照以上方式,可以將外部建的SQLite資料庫成功的發布出來
F. 如何在移動APP中,創建sqlite資料庫時在指定文件位置
要在Android系統中操作SQLite資料庫,是通過Android的核心類SQLiteDatabase類來實現的,通常情況下為了資料庫升級的需要以及使用方便,會選擇繼承SQLiteOpenHelper抽像類,但是SQLiteOpenHelper會將資料庫文件創建在一個固定的目錄(內存的/data/data/<package name/databases>目錄中),如果想使用已經存在的資料庫文件也就是說資料庫會和程序一起發布,就得通過使用SQLiteDabase的靜態方法OpenOrCreateDatabase()方法來得到SQLiteDabase對象,下面是一個具體操作類:
package net.my.;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import net.my.jokebook.R;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
public class DBHelper {
//得到SD卡路徑
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/joke";
private final Activity activity;
//資料庫名
private final String DATABASE_FILENAME;
public DBHelper(Context context) {
// TODO Auto-generated constructor stub
//這里直接給資料庫名
DATABASE_FILENAME = "jokebook.db3";
activity = (Activity)context;
}
//得到操作資料庫的對象
public SQLiteDatabase openDatabase()
{
try
{
boolean b = false;
//得到資料庫的完整路徑名
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
//將資料庫文件從資源文件放到合適地方(資源文件也就是資料庫文件放在項目的res下的raw目錄中)
//將資料庫文件復制到SD卡中 File dir = new File(DATABASE_PATH);
if (!dir.exists())
b = dir.mkdir();
//判斷是否存在該文件
if (!(new File(databaseFilename)).exists())
{
//不存在得到資料庫輸入流對象
InputStream is = activity.getResources().openRawResource(
R.raw.jokebook);
//創建輸出流
FileOutputStream fos = new FileOutputStream(databaseFilename);
//將數據輸出
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}
//關閉資源
fos.close();
is.close();
}
//得到SQLDatabase對象
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return null;
}
}
寫完這個類之後,就能得到SQLiteDatabase對象,就能對資料庫操作了。