2013-04-01 50 views
1

因爲我需要預填充sqlite數據庫,我該如何操作數據庫?像創建一個表並插入一些行?android:如何操作sqlite數據庫而不運行模擬器?

+0

什麼是你的問題..你能詳細解釋一下嗎? – ckpatel

+0

http://www.sqlite.org/ - 您可以下載所有主要平臺的二進制文件,並從PC或Mac創建/填充數據庫。 – 323go

+0

你只是下載sqllite瀏覽器到你的電腦,並認爲你想要什麼? – duggu

回答

2

您可以使用可用的Android SDK的SQLite3的工具。它位於SDK根目錄下的工具文件夾中。

  1. 添加以下目錄的路徑:{SDK根} \工具

  2. 打開命令窗口

  3. 類型的sqlite3

0

從你我的問題認爲你不熟悉Android中的數據庫處理。 你可以很容易地使用一個數據庫處理器類和DatabaseHelper(延長SQLiteOpenHelper

下面的代碼解釋了它很好的如何使用這個類來完成所有的數據庫操作

import java.util.ArrayList; 
import java.util.List; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHandler{ 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "Fruitsdb"; 
    private static final String TABLE_FRUITS = "fruits_table"; 
    private static final String KEY_ID = "id"; 
    private static final String KEY_NAME = "name"; 
    private static final String CREATE_FRUITS_TABLE = "CREATE TABLE " + TABLE_FRUITS + " (" 
      + KEY_ID + " INTEGER PRIMARY KEY autoincrement, " + KEY_NAME + " TEXT not null);"; 

    private SQLiteDatabase db; 
    private final Context ct; 
    private DatabaseHelper dbhelper; 

    public DatabaseHandler(Context context){ 
     this.ct=context; 
     dbhelper=new DatabaseHelper(ct,DATABASE_NAME,null,DATABASE_VERSION); 
    } 
    public DatabaseHandler open(){ 
     db=dbhelper.getWritableDatabase(); 
     return this; 
    } 
    public void close(){ 
     db.close(); 
    } 
    private static class DatabaseHelper extends SQLiteOpenHelper{ 
     public DatabaseHelper (Context ct,String fruitname,CursorFactory cf,int ver){ 
      super(ct,fruitname,cf,ver); 
     } 
     //this oncreate method is called only once when the database is created 
     //and this inserts the values into the tables the first time the app is started 
     @Override 
     public void onCreate(SQLiteDatabase sqldb) { 
      sqldb.execSQL(CREATE_FRUITS_TABLE); 
      List <String> lstfruits=new ArrayList<String>(); 
      lstfruits.add("Orange"); 
      lstfruits.add("Apple"); 
      lstfruits.add("Guava"); 
      lstfruits.add("Grapes"); 
       for(String strFruits:lstfruits){ 
       ContentValues cv=new ContentValues(); 
       cv.put(KEY_NAME, strFruits); 
       sqldb.insert(TABLE_FRUITS, null, cv); 
      } 
     } 
      @Override 
     public void onUpgrade(SQLiteDatabase sqldb, int oldVersion, int newVersion) { 
      sqldb.execSQL("DROP TABLE OF EXISTS"+TABLE_FRUITS); 
      onCreate(sqldb); 
     } 
    } 
    public long insertEntry(String strFruit){ 
     //write code here of insert and change the return statement 
      //using sqldb.insert(parameters..); 
     return -1; 
    } 
    public boolean removeEntry(String strFruit){ 
     //write code here of delete a entery and change the return statement 
      //using sqldb.delete(parameters..); 
     return true; 
    } 
} 

您可以從您的主要活動調用此課程通過

DatabaseHandler dbhandler=new DatabaseHandler(this); 
dbhandler.open(); //The database is created and the values are inserted 
//here you can do insert, updated ,remove,select operations 
dbhandler.close(); // don't forget his one 

這是訪問和存儲sqlite3數據庫中最簡單的方法。希望它能解決你的問題。

相關問題