2010-04-28 103 views
28

我需要一步一步的過程來在Android中創建數據庫。如何在android中創建數據庫?

+0

有關更多信息,我建議您閱讀http://developer.android.com/training/basics/data-storage/databases.html上的android文檔。它一步一步教你一切。祝一切順利 – user2070775 2013-02-14 18:51:28

回答

6

所有的數據訪問創建的數據庫:

db= this.openOrCreateDatabase("db_hw3", MODE_PRIVATE, null); 
45

數據庫是編程重要的事情。我們的許多代碼總是使用數據進行處理和保存。就像任何其他編程環境一樣,Android也支持數據庫編程。您可以使用android支持的默認數據庫SQLiteDatabase

SQLiteDatabase中的數據庫可以包含多個表。假設我們有一個數據庫PERSONALDB和一個表BIODATA。的BIODATA結構是:

_id integer 
code string 
name string 
gender string 

_id is for key increment, 

codenamegender是用於person描述。

當程序第一次被調用時,我們必須確保數據庫和表被打開(如果存在)。否則,我們必須創建一個數據庫和一個表。作爲來自Android notepad sample的示例,這裏是用於操作表Biodata的類PersonDbHelper

import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.List; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.util.Log; 

public class PersonDbHelper { 
    class Row extends Object { 
     public long _Id; 
     public String code; 
     public String name; 
     public String gender; 
    } 

    private static final String DATABASE_CREATE = 
     "create table BIODATA(_id integer primary key autoincrement, " 
      + "code text not null," 
      + "name text not null" 
      +");"; 

    private static final String DATABASE_NAME = "PERSONALDB"; 

    private static final String DATABASE_TABLE = "BIODATA"; 

    private static final int DATABASE_VERSION = 1; 

    private SQLiteDatabase db; 

    public PersonDbHelper(Context ctx) { 
     try { 
      db = ctx.openDatabase(DATABASE_NAME, null); 
     } catch (FileNotFoundException e) { 
      try { 
       db = 
        ctx.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0, 
         null); 
       db.execSQL(DATABASE_CREATE); 
      } catch (FileNotFoundException e1) { 
       db = null; 
      } 
     } 
    } 

    public void close() { 
     db.close(); 
    } 

    public void createRow(String code, String name) { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put("code", code); 
     initialValues.put("name", name); 
     db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    public void deleteRow(long rowId) { 
     db.delete(DATABASE_TABLE, "_id=" + rowId, null); 
    } 

    public List<Row> fetchAllRows() { 
     ArrayList<Row> ret = new ArrayList<Row>(); 
     try { 
      Cursor c = 
       db.query(DATABASE_TABLE, new String[] { 
        "_id", "code", "name"}, null, null, null, null, null); 
      int numRows = c.count(); 
      c.first(); 
      for (int i = 0; i < numRows; ++i) { 
       Row row = new Row(); 
       row._Id = c.getLong(0); 
       row.code = c.getString(1); 
       row.name = c.getString(2); 
       ret.add(row); 
       c.next(); 
      } 
     } catch (SQLException e) { 
      Log.e("Exception on query", e.toString()); 
     } 
     return ret; 
    } 

    public Row fetchRow(long rowId) { 
     Row row = new Row(); 
     Cursor c = 
      db.query(true, DATABASE_TABLE, new String[] { 
       "_id", "code", "name"}, "_id=" + rowId, null, null, 
       null, null); 
     if (c.count() > 0) { 
      c.first(); 
      row._Id = c.getLong(0); 
      row.code = c.getString(1); 
      row.name = c.getString(2); 
      return row; 
     } else { 
      row.rowId = -1; 
      row.code = row.name= null; 
     } 
     return row; 
    } 

    public void updateRow(long rowId, String code, String name) { 
     ContentValues args = new ContentValues(); 
     args.put("code", code); 
     args.put("name", name); 
     db.update(DATABASE_TABLE, args, "_id=" + rowId, null); 
    } 
    public Cursor GetAllRows() { 
     try { 
      return db.query(DATABASE_TABLE, new String[] { 
        "_id", "code", "name"}, null, null, null, null, null); 
     } catch (SQLException e) { 
      Log.e("Exception on query", e.toString()); 
      return null; 
     } 
    } 
} 

在該方法中onCreate,你只要把下面給出一個命令初始化數據庫: ...

Db = new PersonDbHelper(this); 

...

它會嘗試打開'PersonalDB'first.If它不存在,那麼它將創建一個數據庫。在這個'PersonDbHelper'類中,你有插入,刪除,更新,查詢表的方法。

這裏是參考上面的教程:

Creating my first android Database

您可能也經歷:

http://www.devx.com/wireless/Article/40842

http://www.vogella.de/articles/AndroidSQLite/article.html

希望這有助於。