gpt4 book ai didi

android - sqlite3_open_v2 ("/data/data/com.food.find/databases/mydatabase", &handle, 2, NULL) 在android中失败?

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

我正在尝试从 Assets 创建数据库,但出现无法打开数据库的错误但是在我再次运行之后没有给出错误是完成运行第一次创建时给我错误,我把我的 logcat 放在这里

07-04 18:28:00.860: E/Database(990): sqlite3_open_v2("/data/data/com.temp.find/databases/tempfind", &handle, 2, NULL) failed
07-04 18:28:00.870: W/System.err(990): android.database.sqlite.SQLiteException: unable to open database file
07-04 18:28:00.880: W/System.err(990): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
07-04 18:28:00.880: W/System.err(990): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1849)
07-04 18:28:00.880: W/System.err(990): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
07-04 18:28:00.880: W/System.err(990): at com.food.find.RestaurantDatabase.checkDataBase(RestaurantDatabase.java:97)
07-04 18:28:00.890: W/System.err(990): at com.food.find.RestaurantDatabase.createDataBase(RestaurantDatabase.java:60)
07-04 18:28:00.890: W/System.err(990): at com.food.find.SplashScreenActivity.onCreate(SplashScreenActivity.java:25)
07-04 18:28:00.890: W/System.err(990): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-04 18:28:00.890: W/System.err(990): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-04 18:28:00.890: W/System.err(990): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-04 18:28:00.890: W/System.err(990): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-04 18:28:00.900: W/System.err(990): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-04 18:28:00.900: W/System.err(990): at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 18:28:00.900: W/System.err(990): at android.os.Looper.loop(Looper.java:123)
07-04 18:28:00.900: W/System.err(990): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-04 18:28:00.900: W/System.err(990): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 18:28:00.900: W/System.err(990): at java.lang.reflect.Method.invoke(Method.java:507)
07-04 18:28:00.900: W/System.err(990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-04 18:28:00.900: W/System.err(990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-04 18:28:00.910: W/System.err(990): at dalvik.system.NativeStart.main(Native Method)
07-04 18:28:01.030: E/running(990): database opened page size

RestaurantDatabase.java

public class RestaurantDatabase extends SQLiteOpenHelper{


// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_FOODADDRESS="address";
private static final String KEY_FOODPHONE = "phonenumber";
private static final String KEY_LATITUDE="latitude";
private static final String KEY_LONGITUDE="longitude";



private static final String TAG_LAT="lat";
private static final String TAG_LONG="lng";
private static final String TAG_RESPONSE = "responseData";
private static final String TAG_TITLENOFORMATING = "titleNoFormatting";
private static final String TAG_ADDRESSLINES="addressLines";
private static final String TAG_PHONENUMBERS="phoneNumbers";
private static final String TAG_NUMBER="number";
private static final String TAG_FOOD="food";
private static String DB_PATH ="/data/data/com.temp.find/databases/";
private static String DB_NAME ="tempfind";
private SQLiteDatabase myDataBase;
private final Context myContext;

String query = "";

public RestaurantDatabase(Context context)
{
super(context, DB_NAME, null, 1);
this.myContext=context;
};

public void createDataBase() throws IOException
{

boolean dbExist = checkDataBase();

if(dbExist)
{

}
else
{


this.getReadableDatabase();

try
{

copyDataBase();

}
catch (IOException e)
{

throw new Error("Error copying database");

}

}


}


private boolean checkDataBase()
{

SQLiteDatabase checkDB = null;

try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

}
catch(SQLiteException e)
{
e.printStackTrace();
}

if(checkDB != null)
{
checkDB.close();
}

return checkDB != null ? true : false;
}


private void copyDataBase() throws IOException
{


InputStream myInput = myContext.getAssets().open(DB_NAME);

String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);

//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();

}

public RestaurantDatabase(Context context, String name, CursorFactory factory,int version)
{
super(context, name, factory, version);
this.myContext=context;
}


public void openDataBase() throws SQLException
{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
Log.e("running", "database opened page size");

}

@Override
public synchronized void close()
{

if(myDataBase != null)
myDataBase.close();
super.close();

}



@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{


}

public String addContact(String title,String address,String phone,String latitude,String longitude)
{

String check = null;

Log.e("Title","--->"+title);
Log.e("Address","-->"+address);
Log.e("Number","-->"+phone);
Log.e("Latitude","-->"+latitude);
Log.e("Longitude","-->"+longitude);

ContentValues values = new ContentValues();
values.put(KEY_NAME,title); // Contact Name
values.put(KEY_FOODADDRESS,address); // Contact Address
values.put(KEY_FOODPHONE,phone); // Contact Phone
values.put(KEY_LATITUDE,latitude); // Contact Latitude
values.put(KEY_LONGITUDE,longitude); // Contact Longitude

Log.e("addcontact called","--->");

// Inserting Row
myDataBase.insert(TAG_FOOD, null,values);

String selectQuery = " SELECT * FROM " + TAG_FOOD ;

Cursor cursor = myDataBase.rawQuery(selectQuery, null);

Log.e("CURSOR RECORD","--->"+cursor.getCount());
Log.e("INSERT RECORD","--->"+values);


return check;


}



}

最佳答案

这段代码很可怕。我会先删除整个内容,然后重新开始。

首先,如果您要将数据库与您的应用打包在一起,请不要为此编写您自己的代码。有 an existing library, SQLiteAssetHelper ,即经过测试、调试和支持。更重要的是,它是由具有 很多 Android 开发经验的人 (Jeff Gilfelt) 编写的。

其次,如果您正在使用 SQLiteOpenHelper(或像 SQLiteAssetHelper 这样的子类),并且您正在编写 openDatabase( ) 该数据库的任何地方,你做错了SQLiteOpenHelper 背后的完整要点是让为您打开数据库,以便它可以在需要时创建或升级该数据库。

第三,如果您正在使用 SQLiteOpenHelper(或像 SQLiteAssetHelper 这样的子类),并且您有一个空的 empty onCreate( ) 方法,你做错了。这里需要有代码来实际创建数据库,无论是您继承的代码(例如,从 SQLiteAssetHelper)还是您编写的代码。

第四,如果您正在使用 SQLiteOpenHelper(或 SQLiteAssetHelper 之类的子类),并且您正在调用 getReadableDatabase() getWriteableDatabase()忽略返回值您做错了。您调用这些方法来打开数据库,为您提供 SQLiteDatabase 以供使用。

此代码还有很多错误,但这应该足以让您更接近合理的实现。

关于android - sqlite3_open_v2 ("/data/data/com.food.find/databases/mydatabase", &handle, 2, NULL) 在android中失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17471764/

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