我正在玩SQLite和android。到目前为止,我的应用程序有两个活动..
主要活动:
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
public class HomeFavesActivity extends Activity {
private static final String TAG = "HomeFavesCatovoty";
SQLiteDatabase lcDB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "CREATED");
DataBaseManager db = new DataBaseManager(this);
db.getWritableDatabase();
}
}
和dataBaseManager类:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DataBaseManager extends SQLiteOpenHelper{
private static final String TAG = "DataBaseManager";
static final String dbName ="LCInstore";
static final String allIcons = "Icons";
static final String homeIcons = "HomeScreenIcons";
static final String colIconID = "IconID";
static final String colID = "ID";
static final String colImage = "IconImage";
static final String colLabel = "IconLabel";
static final String colName = "IconName";
public DataBaseManager(Context context) {
super(context, dbName, null, 4); // starting value is 1 must change on upgrades
Log.v(TAG, "Initaited");
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.v(TAG, "on create called");
// Create Icon Table if does not exist
db.execSQL("CREATE TABLE "+ allIcons +"" +
"("+colIconID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
colName + " TEXT," +
colImage + " TEXT," +
colLabel + "TEXT)");
// Create HomeScreen Icons Table if does not exist
db.execSQL("CREATE TABLE IF NOT EXIST " + homeIcons+"" +
"("+colID+" INTEGER NOT NULL, FOREIGN KEY ("+colID+") REFERENCES " +
""+allIcons+" ("+colIconID+"));");
InsertIcons(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.v(TAG, "on upgrade called");
db.execSQL("DROP TABLE IF EXISTS "+allIcons);
InsertIcons(db);
}
private void InsertIcons(SQLiteDatabase db) {
ContentValues cv=new ContentValues();
cv.put(colIconID, 1);
cv.put(colName, "Icon1");
cv.put(colImage, "icon_one");
cv.put(colLabel, "ONE");
db.insert(allIcons, colIconID, cv);
cv.put(colIconID, 2);
cv.put(colName, "Icon2");
cv.put(colImage, "icon_two");
cv.put(colLabel, "TWO");
db.insert(allIcons, colIconID, cv);
cv.put(colIconID, 3);
cv.put(colName, "Icon3");
cv.put(colImage, "icon_three");
cv.put(colLabel, "THREE");
db.insert(allIcons, colIconID, cv);
db.close();
}
public void AddHomeScreenIcon(int id){
SQLiteDatabase db=this.getWritableDatabase();
Cursor c = db.rawQuery("select * from "+ homeIcons +" where colID = " +id, null);
int numFound = c.getCount();
if(numFound<1){
ContentValues cv=new ContentValues();
cv.put(colID, id);
db.insert(homeIcons, colID, cv);
}
// else do nothing
}
public void DeleteHomeScreenIcon (int id){
SQLiteDatabase db=this.getWritableDatabase();
db.delete(homeIcons, "id = " +id, null);
// HELP
//Need to find row of homeIcons table that matches id and delete it
db.close();
}
}
你会注意到我创建了4个日志..一个在主要活动'创建,一个在DataBaseManager的构造函数中,它的onCreate,以及它的onUpgrade我正在使用这些日志来尝试查看数据库的创建时间和/或升级ect ..到目前为止,唯一触发的日志是HomeFavesActivity和DataBaseManager的构造函数中的日志。另外两个没有开火。那么我如何实际创建数据库,然后如何升级呢?
EXCEPTION ERROR:
03-07 12:06:33.459: D/AndroidRuntime(1257): Shutting down VM
03-07 12:06:33.459: W/dalvikvm(1257): threadid=1: thread exiting with uncaught exception (group=0x401df760)
03-07 12:06:33.459: E/AndroidRuntime(1257): FATAL EXCEPTION: main
03-07 12:06:33.459: E/AndroidRuntime(1257): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sapient/com.sapient.HomeFavesActivity}: android.database.sqlite.SQLiteException: near "EXIST": syntax error
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.os.Looper.loop(Looper.java:132)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.app.ActivityThread.main(ActivityThread.java:4028)
03-07 12:06:33.459: E/AndroidRuntime(1257): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 12:06:33.459: E/AndroidRuntime(1257): at java.lang.reflect.Method.invoke(Method.java:491)
03-07 12:06:33.459: E/AndroidRuntime(1257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-07 12:06:33.459: E/AndroidRuntime(1257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-07 12:06:33.459: E/AndroidRuntime(1257): at dalvik.system.NativeStart.main(Native Method)
03-07 12:06:33.459: E/AndroidRuntime(1257): Caused by: android.database.sqlite.SQLiteException: near "EXIST": syntax error
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.database.sqlite.SQLiteStatement.native_executeSql(Native Method)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:89)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1933)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1864)
03-07 12:06:33.459: E/AndroidRuntime(1257): at com.sapient.DataBaseManager.onCreate(DataBaseManager.java:45)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
03-07 12:06:33.459: E/AndroidRuntime(1257): at com.sapient.HomeFavesActivity.onCreate(HomeFavesActivity.java:22)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-07 12:06:33.459: E/AndroidRuntime(1257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
03-07 12:06:33.459: E/AndroidRuntime(1257): ... 11 more
答案 0 :(得分:0)
当您在getWritableDatabase()
实例 db 上调用getReadableDatabse()
或DataBaseManager
时,您将输入这些方法。
db.getWritableDatabase();
或
db.getReadableDatabase();