2012-07-21 88 views
0

我使用SQLite構建了一個數據庫,當我嘗試運行填充數據庫的主活動文件時,它在onCreate上崩潰。我無法加載該活動。我的onCreate方法使用getWriteableDatabase打開一個數據庫,並且我只是將值className,workoutName和id添加到我創建的表中,這些表會執行這些值。出於某種原因,我的活動甚至無法加載。有任何想法嗎?onCreate()的活動崩潰

下面是我的代碼:

import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Intent; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class poetry3 extends Activity { 

Button next; 
ClassName addName; 
Preference prefs; 
DBHelper1 dbHelper; 
SQLiteDatabase db; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.poetry3); 
    dbHelper = new DBHelper1(this); 
    String className = "edu.njit.hss.poetry3"; 
    String workoutName = "Poetry 3"; 
    Calendar currentDate = Calendar.getInstance(); 
    String date = currentDate.getTime().toString(); 

    int classID = 1; 


    db = dbHelper.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 


    //if(values.size() > 25) 

    values.clear(); 
    values.put(DBHelper1.C_ID, classID); 
    values.put(DBHelper1.C_CLASSNAME, className); 
    values.put(DBHelper1.C_WORKOUT_NAME, workoutName); 
    values.put(DBHelper1.C_CREATED_AT, date); 
    db.insertOrThrow(DBHelper1.TABLE, null, values); 



    next = (Button)findViewById(R.id.Continue); 
    next.setOnClickListener(phaseHandler); 

} 

View.OnClickListener phaseHandler = new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if(next.getId() == ((Button)v).getId()){ 
      Intent i = new Intent(poetry3.this, poetry5.class); 
      startActivity(i); 
     } 

    } 
}; 

    } 

這裏是logcat的輸出:

07-21 18:57:26.628: ERROR/AndroidRuntime(695): FATAL EXCEPTION: main 
07-21 18:57:26.628: ERROR/AndroidRuntime(695): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.njit.hss/edu.njit.hss.poetry3}: android.database.sqlite.SQLiteException: near "at": syntax error: , while compiling: INSERT INTO history(workout_name, created at, _ID, classname) VALUES(?, ?, ?, ?); 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.os.Looper.loop(Looper.java:123) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.app.ActivityThread.main(ActivityThread.java:3647) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at java.lang.reflect.Method.invoke(Method.java:507) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at dalvik.system.NativeStart.main(Native Method) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695): Caused by: android.database.sqlite.SQLiteException: near "at": syntax error: , while compiling: INSERT INTO history(workout_name, created at, _ID, classname) VALUES(?, ?, ?, ?); 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1444) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at edu.njit.hss.poetry3.onCreate(poetry3.java:49) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586) 
07-21 18:57:26.628: ERROR/AndroidRuntime(695):  ... 11 more 
07-21 18:57:26.658: WARN/ActivityManager(39): Force finishing activity edu.njit.hss/.poetry3 
+2

它是否強制關閉?如果是這樣,請發佈崩潰的LogCat日誌。 – Eric 2012-07-21 18:35:46

+0

LogCat需要.. – 2012-07-21 18:38:26

+0

如果你想要一個解決方案,你也必須給你的LogCat。 – bmavus 2012-07-21 18:42:16

回答

1

的錯誤是在你的數據庫的創建表語句(以DBHelper1.onCreate()):

create table history(BaseColumns._ID int primary key, classname text workout_nametext) 

如果你想在引用該id在ContentProvider的(如ContactsContract,CalendarContract等)瞭解到, BaseColumns._ID意味着SQL中的一些非常不同的東西;你也錯過了逗號和空格。我假設你的意思是:

create table history(_id integer primary key, classname text, workout_name text) 

如果您張貼在那裏你創建你的SQLite數據庫的字符串,我可以更具體介紹一下語法...

String table_create = "create table " + table_name + " (" + BaseColumns._ID + ... 

新的logcat

要閱讀LogCat,經常要查找最低的「由...引起」,在這種情況下:

Caused by: android.database.sqlite.SQLiteException: near "at": syntax error: , while compiling: INSERT INTO history(workout_name, created at, _ID, classname) VALUES(?, ?, ?, ?); 

這發生在poetry3.onCreate(),特別是在第49行

在這裏你可以看到,導致錯誤「在創造」,SQLite的變量定義爲一個字的名字,也許你的意思created_at?但您的創建聲明只有三列,所以我只是將其刪除:

INSERT INTO history(_ID, classname, workout_name) VALUES(?, ?, ?, ?); 
+0

String sql =「create table」+ TABLE +「(」+ C_ID +「int primary key,」+ \t \t C_CLASSNAME +「text」+ C_WORKOUT_NAME +「文字)」; – 2012-07-21 19:01:14

+0

我還添加了新的LogCat輸出。感謝您的幫助順便說一句。 – 2012-07-21 19:01:47

+0

我的新字符串看起來像這樣,但它說我沒有列created_at?String sql =「create table」+ TABLE +「(」+ C_ID +「int primary key」,+ \t \t C_CLASSNAME +「text」+ C_CREATED_AT +「int」+ C_WORKOUT_NAME +「text)」; – 2012-07-21 19:17:13

2

這裏的錯誤:

07-21 18:39:32.788: ERROR/Database(507): Failure 1 (near ".": syntax error) on 0x1e0058 when preparing 'create table history(BaseColumns._ID int primary key, classname text workout_nametext)'. 

你需要一個空間和一個逗號: 「classname text,workout_name text」。

+0

感謝您的幫助。其他人提到我做了更改併發布了新的LogCat輸出 – 2012-07-21 19:02:36