我有这个数据库代码和我想写入数据库的文件。但是当试图在filexplorer中找到数据库时,没有数据库。在logcat我得到了这个:
02-04 21:16:02.722: INFO/Database(3361): sqlite returned: error code = 14, msg = cannot open file at source line 25467
02-04 21:16:02.722: ERROR/Database(3361): sqlite3_open_v2("/data/data/com.sr.android/databases/myapp", &handle, 6, NULL) failed
为什么我没有任何数据库?错误在哪里?
public class Main extends TabActivity {
DataBaseHandler db = new DataBaseHandler(this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getBrands();
private boolean getBrands() {
JsonGetBrands reader = new JsonGetBrands("testa2.php");
String Brand = reader.getBrands();
System.out.println("hej plus " +Brand);
db.open();
long id = 0 ;
db.InsertBrand(Brand);
id = db.getAllEntries();
Context context = getApplicationContext();
CharSequence text = "The Brands" + Brand +"har lagts till i databasen\n brands total= " +id;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
db.close();
int id = 0;
public static final String KEY_ROWID = "_id";
public static final String KEY_BRAND = "Brand";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "mydb";
private static final String DATABASE_TABLE = "tblBrands";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table tblBrands (_id integer primary key autoincrement, "
+ "Brand text not null );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DataBaseHandler(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS tblBrands");
onCreate(db);
}
}
//---opens the database---
public DataBaseHandler open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long InsertBrand(String Brand)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_BRAND, Brand);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public int getAllEntries()
{
Cursor cursor = db.rawQuery(
"SELECT COUNT(Brand) FROM tblBrands", null);
if(cursor.moveToFirst()) {
return cursor.getInt(0);
}
return cursor.getInt(0);
}
public String getRandomEntry()
{
id = getAllEntries();
Random random = new Random();
int rand = random.nextInt(getAllEntries());
if(rand == 0)
++rand;
Cursor cursor = db.rawQuery(
"SELECT Brand FROM tblBrands WHERE _id = " + rand, null);
if(cursor.moveToFirst()) {
return cursor.getString(0);
}
return cursor.getString(0);
}
}
答案 0 :(得分:1)
我在AVD模拟器上遇到了同样的问题。 对我有用的解决方案是使用应用程序/管理应用程序从应用程序列表中选择应用程序卸载应用程序,然后在模拟器上点击卸载选项,然后重新安装应用程序,一切都很好。一切正常!
答案 1 :(得分:0)
您将收到错误
error code = 14, msg = cannot open file at source line 25467 02-04 21:16:02.722: ERROR/Database(3361): sqlite3_open_v2
仅当您未在模拟器中创建数据库时。您可以检查是否在IDE DDMS / file_explorer(/data/data/Ur_pkg_name/databases/ur_db_name
)中创建了数据库。