在我的android應用程序中,我使用SQLite數據庫。它僅適用於模擬器。當我將apk文件安裝到設備中時,它不會顯示任何數據。但在我的模擬器中,它工作得很好。我無法理解這個問題。帶設備的SQLite數據庫
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
}
}
}
發佈一些代碼。 – Anukool
發佈一些代碼,或更多的說明,而不是「它不會顯示任何數據」。 – Jack
我使用這麼多活動,並且我用數據庫做了很多事情。我應該發佈哪一個?我的問題是它在仿真器上運行良好。爲什麼不在設備上? –