带有设备的SQLite数据库

时间:2013-02-01 05:26:03

标签: android android-sqlite sqlite

在我的Android应用程序中,我使用SQLite数据库。它仅适用于模拟器。当我将apk文件安装到设备中时,它不会显示任何数据。但在我的模拟器中,它的效果非常好。我无法理解这个问题。

1)我是否将任何内容复制到设备上以使其正常工作?

2)我创建的数据库是否共同?

3)我是否为每个新用户备份数据库?

请有人解释

public class SQLiteAdapterv {

public static final String MYDATABASE_NAME2 = "MY_DATABASE2";
public static final String MYDATABASE_TABLE2 = "MY_VLGMAS";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT1 = "Content1";
public static final String KEY_CONTENT2 = "Content2";

public static final String KEY_CONTENT3 = "Content3";
public static final String KEY_CONTENT4 = "Content4";
public static final String KEY_VCODE = "vcode";
public static final String KEY_VNAME = "vname";
public static final String KEY_DIVSEC= "divsec";

private static final String SCRIPT_CREATE_DATABASE1 =
          "create table " + MYDATABASE_TABLE2 + " ("
          + KEY_ID + " integer primary key autoincrement, "
          + KEY_VCODE + " text not null, "
          + KEY_VNAME + " text not null,"
          + KEY_DIVSEC + " text not null);";

private SQLiteHelper sqLiteHelper;
private  SQLiteDatabase sqLiteDatabase;
private Context context;

public SQLiteAdapterv(Context c)
{
    context=c;
}
public SQLiteAdapterv openToRead() throws android.database.SQLException {
      sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME2, null, MYDATABASE_VERSION);
      sqLiteDatabase = sqLiteHelper.getReadableDatabase();
      return this; 
     }
public SQLiteAdapterv openToWrite() throws android.database.SQLException {
      sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME2, null, MYDATABASE_VERSION);
      sqLiteDatabase = sqLiteHelper.getWritableDatabase();
      return this; 
     }
public void close(){
      sqLiteHelper.close();
     }
public long insert(String vcode, String vname,String divsec){

      ContentValues contentValues = new ContentValues();
      contentValues.put(KEY_VCODE, vcode);
      contentValues.put(KEY_VNAME, vname);
      contentValues.put(KEY_DIVSEC, divsec);
      return sqLiteDatabase.insert(MYDATABASE_TABLE2, null, contentValues);
     }
public int deleteAll(){
      return sqLiteDatabase.delete(MYDATABASE_TABLE2, null, null);
     }
 public  Cursor queueAll(){
      String[] columns = new String[]{KEY_ID, KEY_VCODE, KEY_VNAME,KEY_DIVSEC};
      Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE2, columns,
        null, null, null, null, null);
      return cursor;
     }
 public String getDivSec(String vname)
 {
     Cursor c=sqLiteDatabase.rawQuery("SELECT * FROM " + MYDATABASE_TABLE2 + " WHERE "+KEY_VNAME +"='"+ vname+"'",null);
     c.moveToFirst();
     String divsec="";
     if (c!=null)
     {
        divsec=c.getString(3);
     }
     c.close();
     return divsec;
 }

 public String[] getAllVillage()
 {
     int i=0;
     Cursor c=sqLiteDatabase.rawQuery("SELECT * FROM " + MYDATABASE_TABLE2 ,null);
     String[] villagelist=new String[c.getCount()];
     if (c.moveToFirst())
     {
         do
         {
             villagelist[i]=c.getString(2);
             i++;
         }while(c.moveToNext());
     }
     c.close();
    return villagelist;
 }
 public String getVcode(String vname)
 {
     Cursor c=sqLiteDatabase.rawQuery("SELECT * FROM " + MYDATABASE_TABLE2 + " WHERE "+KEY_VNAME +"='"+ vname+"'",null);
     c.moveToFirst();
     String vcode="";
     if (c!=null)
     {
        vcode=c.getString(1);
     }
     c.close();
     return vcode;
 }

 public class SQLiteHelper extends SQLiteOpenHelper
 {

    public SQLiteHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(SCRIPT_CREATE_DATABASE1);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

 }

}

2 个答案:

答案 0 :(得分:1)

通常Sqlite数据存储在

 //data/data/<Your-Application-Package-Name>/databases/<your-database-name>

但除非您具有超级用户权限,否则无法看到它们。 因此,您可以将数据库直接存储在SD卡中,如下所示:

 static class  SQLiteHelper extends SQLiteOpenHelper {

 SQLiteHelper(final Context context) {
    super(context, Environment.getExternalStorageDirectory()
            + File.separator + "/DataBase/" + File.separator
            + DATABASE_NAME, null, DATABASE_VERSION);
}

现在您可以在/ sdcard的DataBase文件夹中看到创建的数据库 如果您将数据库存储在内部存储器中,那么您也可以将ur数据库从内部存储器复制到SD卡:)

答案 1 :(得分:0)

是的,您需要将该数据库复制到Android设备。请参阅此内容     tutorial解决方案。