0

在我的 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

    }

 }

}
4

2 回答 2

1

Normally Sqlite data are stored in

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

But you can't see them unless you have root access. So You can store your database directly in SD card like this:

 static class  SQLiteHelper extends SQLiteOpenHelper {

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

and now you can see created database in DataBase folder in /sdcard If you store ur databse in internal memory then you can also copy ur database from internal memory to SD card :)

于 2013-02-01T06:22:28.333 回答
0

是的,您需要将该数据库复制到 android 设备。有关解决方案,请参阅本 教程

于 2013-02-01T05:35:10.080 回答