android——建立資料庫

來源:互聯網
上載者:User

SQLlite類似於微軟的Access.以下介紹在android中建立一個資料庫檔案

1、DBOpenHelper

package com.njupt.sqlite;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class DBOpenHelper extends SQLiteOpenHelper{public DBOpenHelper(Context context){super(context, "njupt1.db", null, 2);}public void onCreate(SQLiteDatabase db) {//檔案 不存在時執行System.out.println("onCreate······章澤天是我的女神1");db.execSQL("CREATE TABLE person1(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//檔案存在,版本提升時執行System.out.println("onUpdate····我的女神是章澤天1");db.execSQL("ALTER TABLE person1 ADD balance INTEGER");}}

2、DBTest

package com.njupt.sqlite;import android.test.AndroidTestCase;public class DBTest extends AndroidTestCase{public void test1(){DBOpenHelper helper = new DBOpenHelper(getContext());helper.getWritableDatabase();//擷取可寫的資料庫連接}}

3、結果如下所示:

1)

解釋:第一條日誌是在第一次執行時輸出的。第二條日誌是在實在資料庫版本升級時輸出的。

2)

解釋:建立資料庫成功以後,你就會在/data/data/本項目下發現一個新建立的名為databases的目錄,你所建立的資料庫檔案就儲存在databases目錄下

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.