0

我使用 SQLite 构建了一个数据库,当我尝试运行填充数据库的主活动文件时,它在 onCreate 上崩溃。我无法加载活动。我的 onCreate 方法使用 getWriteableDatabase 打开一个数据库,然后我只需将值 className、锻炼名称和 id 添加到我创建的执行这些值的表中。出于某种原因,我的活动甚至不会加载。有任何想法吗?

下面是我的代码:

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
4

2 回答 2

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)'.

您需要一个空格和一个逗号:“类名文本,锻炼名称文本”。

于 2012-07-21T18:49:46.830 回答
1

错误出现在数据库的创建表语句中(在 DBHelper1.onCreate() 中):

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

如果您尝试在 ContentProvider 中引用 id(如 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”,在这种情况下:

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 行。

在这里您可以看到“created at”导致错误,SQLite 将变量定义为一个单词名称,也许您的意思是created_at?但是您的 create 语句只有三列,所以我将其删除:

INSERT INTO history(_ID, classname, workout_name) VALUES(?, ?, ?, ?);
于 2012-07-21T18:51:43.320 回答