我的數據庫:ANDROID sqlite的沒有這種表錯誤 - 現有數據的基礎上 -
我dataBaseHelper:
package com.up.ridechaser;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.up.ridechaser/databases/";
private static String DB_NAME = "DB.sqlite";
private static final String TABLE_NAME = "lines";
private static final String DATABASE_CREATE = "CREATE TABLE "+ TABLE_NAME +" (`_id` INTEGER NOT NULL,`linename` VARCHAR,`fromgare` VARCHAR,`togare` VARCHAR, PRIMARY KEY(_id) )";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(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_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
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);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
}
catch(SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS lines");
onCreate(db);
}
private static final String[] COLUMNS = { "_id", "linename", "fromgare", "togare" };
public line getLine(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("lines", COLUMNS, " _id = ?", new String[] { String.valueOf(id) }, null, null, null,
null);
if (cursor != null)
cursor.moveToFirst();
line Line = new line();
Line.setId(Integer.parseInt(cursor.getString(0)));
Line.setLinename(cursor.getString(1));
Line.setFromgare(cursor.getString(2));
Line.setTogare(cursor.getString(3));
// log
Log.d("getline(" + id + ")", Line.toString());
return Line;
}
}
我main_activity:
package com.up.ridechaser;
import java.io.IOException;
import com.up.ridechaser.DataBaseHelper;
import android.app.Activity;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper.getLine(1);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
07- 03 09:03:10.821:W/dalvikvm(679):thr eadid = 1:線程退出時未捕獲的異常(group = 0x409bf1f8)07-03 09:03:10.841:E/AndroidRuntime(679):致命異常:main 07-03 09:03:10.841:E/AndroidRuntime(679) java.lang.RuntimeException:無法啓動活動ComponentInfo {com.up.ridechaser/com.up.ridechaser.MainActivity}:android.database.sqlite.SQLiteException:no such:rows:,while compiling:SELECT id,linename, fromgare,togare FROM lines where where id =? 07-03 09:03:10.841:E/AndroidRuntime(679):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)07-03 09:03:10.841:E/AndroidRuntime(679):在android。 app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)07-03 09:03:10.841:E/AndroidRuntime(679):at android.app.ActivityThread.access $ 600(ActivityThread.java:123)07-03 09:03 :10.841:E/AndroidRuntime(679):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)07-03 09:03:10.841:E/AndroidRuntime(679):在android.os.Handler。 dispatchMessage(Handler.java:99)07-03 09:03:10.841:E/AndroidRuntime(679):at android.os.Looper.loop(Looper.java:137)07-03 09:03:10.841:E/AndroidRuntime(679):在android.app.ActivityThread.main(ActivityThread.java:4424)07-03 09:03:10.841:E/AndroidRuntime(679):at java.lang.reflect.Method.invokeNative(Native Method) 07-03 09:03:10.841:E/AndroidRuntime(679):at java.lang.reflect.Method.invoke(Method.java:511)07-03 09:03:10.841:E/AndroidRuntime(679):at昏迷ndroid.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)07-03 09:03:10.841:E/AndroidRuntime(679):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java :551)07-03 09:03:10.841:E/AndroidRuntime(679):at dalvik.system.NativeStart.main(Native Method)07-03 09:03:10.841:E/AndroidRuntime(679):引起: android.database.sqlite.SQLiteException:沒有這樣的table:lines:,編譯時:SELECT id,linename,fromgare,togare FROM lines WHERE id =? 07-03 09:03:10.841:E/AndroidRuntime(679):at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)07-03 09:03:10.841:E/AndroidRuntime(679):at android.database .sqlite.SQLiteCompiledSql。(SQLiteCompiledSql.java:68)07-03 09:03:10.841:E/AndroidRuntime(679):at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)07-03 09: 03:10.841:E/AndroidRuntime(679):at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)07-03 09:03:10.841:E/AndroidRuntime(679):at android.database.sqlite .SQLiteProgram。(SQLiteProgram.java:127)07-03 09:03:10.841:E/AndroidRuntime(679):at android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:94)07-03 09:03:10.841 :E/AndroidRuntime(679):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:53)07-03 09:03:10.841:E/AndroidRuntime(679):在android.database.sqlite.SQLiteDirectCursorDriver.query (SQLiteDirectCursorDriver.java:47)07-03 09:03:10.841:E/AndroidRuntime(679):at android.databas e.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)07-03 09:03:10.841:E/AndroidRuntime(679):at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)07-03 E:/ AndroidRuntime(679):at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)07-03 09:03:10.841:E/AndroidRuntime(679):at android.database .sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)07-03 09:03:10.841:E/AndroidRuntime(679):at com.up.ridechaser.DataBaseHelper.getLine(DataBaseHelper.java:120)07-03 09 :03:10.841:E/AndroidRuntime(679):在com.up.ridechaser.MainActivity.onCreate(MainActivity.java:21)07-03 09:03:10.841:E/AndroidRuntime(679):在android.app。 Activity.performCreate(Activity.java:4466)07-03 09:03:10.841:E/AndroidRuntime(679):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)07-03 09:03:10.841: E/AndroidRuntime(679):在android.app.ActivityThread.performLaunchActivity(ActivityThread。java:1920)
plz help that that!
錯誤仍然在我的代碼,我找不到它,我嘗試使用現有的sqlite數據庫,這有點難,有些幫助? –
請看這個鏈接 - > http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application –