我正在制作2.3.3版本的Android应用程序,但在将我的应用程序连接到数据库时,它会发出以下错误: 数据库代码如下,请仔细查看并提出最佳更改。
//---------------------------------------------------------------------------------------//
class DataBaseHelper extends SQLiteOpenHelper
{
private static String DB_NAME ="Akhbarwala.sqlite";// Database name
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
private static String DB_PATH = "/sdcard/";
private SQLiteDatabase mDataBase;
private final Context mContext;
DataBaseHelper(final Context context)
{
super(context, Environment.getExternalStorageDirectory()
+ File.separator +
DB_NAME, null, 1);
this.mContext = context;
}
//---------------------------------------------------------------------------------------//
public void createDataBase() throws IOException
{
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
Log.e("Error in Finding Database", "Please Insert the Database in SD Card");
}
}
//----------------------------------------------------------------------------------//
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH+DB_NAME);
return dbFile.exists();
}
//-----------------------------------------------------------------------------//
private void copyDataBase() throws IOException
{
InputStream mInput;
mInput = new FileInputStream(DB_NAME);
File directory = new File(DB_PATH+DB_NAME);
// String outFileName = DB_PATH;
if (!directory.exists()) {
directory.mkdirs();
}
OutputStream mOutput = new FileOutputStream(directory
.getPath() );
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
//----------------------------------------------------------------------------//
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH+DB_NAME;
this.getWritableDatabase();
// mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.OPEN_READWRITE);
mDataBase = SQLiteDatabase.openOrCreateDatabase(mPath, null);
return mDataBase != null;
}
//-----------------------------------------------------------------//
@Override
public synchronized void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}
//-------------------------------------------------------------//
@Override
public void onCreate(SQLiteDatabase arg) {
}
//--------------------------------------------------------------------//
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//-------------------------------------------------------//
}
并且日志文件是
10-30 10:05:28.308: D/dalvikvm(10358): GC_EXTERNAL_ALLOC freed 56K, 51% free 2682K/5379K, external 383K/517K, paused 117ms
10-30 10:05:28.658: E/here(10358): SELECT DISTINCT USER_NAME FROM HAWKER_MST WHERE USER_TYPE = 'H' ORDER BY rowid
10-30 10:05:28.728: I/Database(10358): sqlite returned: error code = 8, msg = prepared statement aborts at 1: [BEGIN EXCLUSIVE;]
10-30 10:05:28.728: E/Database(10358): Failure 8 (attempt to write a readonly database) on 0x91c6f00 when executing 'BEGIN EXCLUSIVE;'
10-30 10:05:28.758: D/AndroidRuntime(10358): Shutting down VM
10-30 10:05:28.758: W/dalvikvm(10358): threadid=1: thread exiting with uncaught exception (group=0xb70bc4f0)
10-30 10:05:28.768: E/AndroidRuntime(10358): FATAL EXCEPTION: main
10-30 10:05:28.768: E/AndroidRuntime(10358): android.database.sqlite.SQLiteException: attempt to write a readonly database: BEGIN EXCLUSIVE;
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.database.sqlite.SQLiteDatabase.beginTransactionWithListener(SQLiteDatabase.java:527)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:481)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:123)
10-30 10:05:28.768: E/AndroidRuntime(10358): at sangi.rawat.akhbaarwala.TestAdapter.getAllLabels(TestAdapter.java:120)
10-30 10:05:28.768: E/AndroidRuntime(10358): at sangi.rawat.akhbaarwala.Akhbar_main.loadSpinnerData(Akhbar_main.java:93)
10-30 10:05:28.768: E/AndroidRuntime(10358): at sangi.rawat.akhbaarwala.Akhbar_main.onItemSelected(Akhbar_main.java:116)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.widget.AdapterView.access$200(AdapterView.java:42)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.os.Handler.handleCallback(Handler.java:587)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.os.Looper.loop(Looper.java:130)
10-30 10:05:28.768: E/AndroidRuntime(10358): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-30 10:05:28.768: E/AndroidRuntime(10358): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 10:05:28.768: E/AndroidRuntime(10358): at java.lang.reflect.Method.invoke(Method.java:507)
10-30 10:05:28.768: E/AndroidRuntime(10358): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-30 10:05:28.768: E/AndroidRuntime(10358): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-30 10:05:28.768: E/AndroidRuntime(10358): at dalvik.system.NativeStart.main(Native Method)
10-30 10:05:30.648: I/Process(10358): Sending signal. PID: 10358 SIG: 9
答案 0 :(得分:0)
您需要以可写方式打开数据库。目前,您将其打开为只读,BEGIN EXCLUSIVE;
语句不起作用