[英]Why am I getting this error when attempting to write to a database?
I have this database code and the file where I want to write to the database. 我有此数据库代码和要写入数据库的文件。 But when trying to find the database in the filexplorer there is no database.
但是,当尝试在filexplorer中查找数据库时,没有数据库。 In logcat I'm getting this:
在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
Why don't I have any database? 为什么我没有任何数据库? Where is the error?
错误在哪里?
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);
}
}
I had the same problem on AVD Emulator. 我在AVD Emulator上遇到了同样的问题。 Solution that worked for me was to uninstall the app using the applications/manage applications choosing the application fro the applications list and hitting the uninstall option on the emulator then re-installed the app and all is good.
对我有用的解决方案是使用应用程序/管理应用程序来卸载应用程序,然后从应用程序列表中选择应用程序,然后在模拟器上单击卸载选项,然后重新安装应用程序,一切都很好。 Everything just worked!
一切都正常!
You will get the error 你会得到错误
error code = 14, msg = cannot open file at source line 25467 02-04 21:16:02.722: ERROR/Database(3361): sqlite3_open_v2
only when your database is not created in the emulator. 仅当未在模拟器中创建数据库时。 You can check whether your database is created in your IDE DDMS/file_explorer (
/data/data/Ur_pkg_name/databases/ur_db_name
). 您可以检查是否在IDE DDMS / file_explorer(
/data/data/Ur_pkg_name/databases/ur_db_name
)中/data/data/Ur_pkg_name/databases/ur_db_name
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.