android:如何在不运行模拟器的情况下操作sqlite数据库?

时间:2013-04-01 04:34:05

标签: android android-sqlite

因为我需要预先填充sqlite数据库,我该如何操作数据库?比如创建一个表并插入一些行?

3 个答案:

答案 0 :(得分:2)

您可以使用Android SDK提供的SQLite3工具。它位于SDK root下的tools文件夹中。

  1. 将以下目录添加到路径: {SDK root} \ tools

  2. 打开命令窗口

  3. 输入sqlite3

答案 1 :(得分:0)

许多工具允许操作sqlite文件。如何使用sqlite odbc驱动程序并使用像微软访问一样的工具(如果你有的话)

http://www.ch-werner.de/sqliteodbc/

答案 2 :(得分:0)

根据您的问题,我认为您不熟悉Android中的数据库处理。 您可以非常轻松地使用 DatabaseHandler 类和 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数据库中访问和存储值的最简单方法。希望它可以解决你的问题。