gpt4 book ai didi

android - 为什么在尝试写入数据库时​​出现此错误?

转载 作者:行者123 更新时间:2023-11-29 02:14:47 25 4
gpt4 key购买 nike

我有这个数据库代码和我想写入数据库的文件。但是当试图在 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);

}

}

最佳答案

我在 AVD 模拟器上遇到了同样的问题。对我有用的解决方案是使用应用程序/管理应用程序卸载应用程序,从应用程序列表中选择应用程序并点击模拟器上的卸载选项,然后重新安装应用程序,一切都很好。一切正常!

关于android - 为什么在尝试写入数据库时​​出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4902575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com