Android之SQLite3命令列管理資料庫

來源:互聯網
上載者:User

SQLite適合行動裝置的資料存放區,有處理速度快,佔用資源少等優點,不需要安裝部署,內嵌到程式中作為其一部分.http://www.sqlite.org/

SQLite的資料庫檔案位於/data/data/your-app-name/databases目錄下.

使用資料庫最基本就是增刪改查操作.下面的樣本是使用SQLite進行增刪改查的操作.

我們需要一個輔助類繼承SQLiteOpenHelper類

package com.sumq;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class MySQLiteHelper extends SQLiteOpenHelper{public MySQLiteHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);// TODO Auto-generated constructor stub}/* *當資料庫被建立時,首次執行該方法 *一般將建立表等初始化操作放在該方法中執行  */@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubdb.execSQL("create table if not exists user_info(id integer primary key,name varchar,vip integer)");}/*當版本升級時調用該函數 *參數一為要更新的資料庫 *參數二傳入老的版本號碼 *參數三傳入新的版本號碼  */@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub}/* *看需要,該方法在每次開啟資料庫時被調用  */@Overridepublic void onOpen(SQLiteDatabase db) {// TODO Auto-generated method stubsuper.onOpen(db);}}

package com.sumq;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.widget.TextView;public class UISQLiteActivity extends Activity {TextView tv;MySQLiteHelper mySQLiteHelper;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tv = (TextView)findViewById(R.id.tv);        mySQLiteHelper = new MySQLiteHelper(this, "test.db", null, 1);        insertAndUpdateData(mySQLiteHelper);        String result = queryData(mySQLiteHelper);        tv.setText("名字\t等級\t"+result);    }        private void insertAndUpdateData(MySQLiteHelper mySQLiteHelper){    //擷取資料庫物件    SQLiteDatabase db = mySQLiteHelper.getWritableDatabase();    //第一種插入資料庫的方法直接寫sql語句    db.execSQL("insert into user_info(name,vip) values('user1',1)");    //第二種調用insert方法    ContentValues values = new ContentValues();    values.put("name", "user2");    values.put("vip", "2");    db.insert("user_info", null, values);    //更新level=2 的資料    values.clear();    values.put("name", "user2");    values.put("vip", "3");    db.update("user_info", values, "vip = ?", new String[]{"2"});        db.close();    }        private String queryData(MySQLiteHelper mySQLiteHelper){    String result ="";    SQLiteDatabase db = mySQLiteHelper.getReadableDatabase();    Cursor cursor = db.query("user_info", null, null, null, null, null, "id asc");    while (cursor.moveToNext()) {    result = result+cursor.getString(cursor.getColumnIndex("name"))+"    ";    result = result+cursor.getInt(cursor.getColumnIndex("vip"))+"    \n";}    cursor.close();    db.close();    return result;    }        @Override    protected void onDestroy() {        SQLiteDatabase db =mySQLiteHelper.getWritableDatabase();        db.delete("user_info", "1", null);    super.onDestroy();    }}

執行程式結果顯示

上面提到我們的資料庫儲存的位置位於/data/data/com.sumq/databases/test.db

下面我們就使用adb shell命名查詢上面的樣本執行完建立的資料庫

1.開啟"開始"->"運行"輸入cmd顯示dos視窗

2.依次執行命名進入我們的資料庫檔案位置下.

adb shell命令是我們已經配置好環境變數 我自己的sdk路徑是這樣的 D:\Program Files\android-sdk-windows\tools所以需要注意自己的是否已經配置好環境變數否則這個命令不能被識別.

cd data是進入相應的檔案夾

ls 顯示檔案夾的所有檔案 加上參數   -l 顯示檔案的顯示資訊

test.db是我們樣本建立的資料庫.現在輸入命令進入資料庫操作

輸入slqite3 test.db 直接進入我們的資料庫,可以發現左邊的顯示sqlite>就是我們可以進入到資料庫操作命令列了.

.table顯示資料庫下的所有表.  android_metadata是系統資料表. 不是我們樣本建立的表. 先不管它

.schema user_info顯示表的結構.

.dump user_info顯示表的結構和內容.

.exit 退出.

我們也可以使用txt匯入資料, 只要規定要格式就是可以.

1.建立一個txt

注意:文本內不要任何的空格,儲存任意檔案名稱.我這裡儲存user_info.

2.將user_info.txt放在相應的databases目錄下.

顯示目錄.

3.執行命令

至此匯入資料成功.

當然除了使用命令,也有圖形的用戶端軟體.比如SQLiteSpy.或者Firefox的SQLite Manager都可以查看資料庫.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.