引起:SQL(查询)错误或缺少数据库

时间:2016-11-30 19:04:00

标签: android sqlite android-sqlite

每次单击按钮向表中添加项目时,应用程序崩溃,我都会收到此错误,logcat:

    11-30 21:46:00.822 13943-13943/com.example.user.timetable_test E/SQLiteLog: (1) near "TABLESunday": syntax error
11-30 21:46:00.827 13943-13943/com.example.user.timetable_test D/AndroidRuntime: Shutting down VM
11-30 21:46:00.827 13943-13943/com.example.user.timetable_test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.user.timetable_test, PID: 13943
                                                                                 android.database.sqlite.SQLiteException: near "TABLESunday": syntax error (code 1): , while compiling: CREATE TABLESunday(_session INTEGER PRIMARY KEY AUTOINCREMENT,_name TEXT,_length INTEGER);
                                                                                 #################################################################
                                                                                 Error Code : 1 (SQLITE_ERROR)
                                                                                 Caused By : SQL(query) error or missing database.
                                                                                    (near "TABLESunday": syntax error (code 1): , while compiling: CREATE TABLESunday(_session INTEGER PRIMARY KEY AUTOINCREMENT,_name TEXT,_length INTEGER);)
                                                                                 #################################################################
                                                                                     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
                                                                                     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
                                                                                     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                                                                                     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1812)
                                                                                     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743)
                                                                                     at com.example.user.timetable_test.setup.TableDBHandler.onCreate(TableDBHandler.java:43)
                                                                                     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                                     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                     at com.example.user.timetable_test.setup.TableDBHandler.addEntry(TableDBHandler.java:63)
                                                                                     at com.example.user.timetable_test.setup.SetTable$PlaceholderFragment$1.onClick(SetTable.java:206)
                                                                                     at android.view.View.performClick(View.java:5697)
                                                                                     at android.widget.TextView.performClick(TextView.java:10826)
                                                                                     at android.view.View$PerformClick.run(View.java:22526)
                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:158)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

SQLitDatabase类:

package com.example.user.timetable_test.setup;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.user.timetable_test.MiscData;


public class TableDBHandler extends SQLiteOpenHelper{

    private MiscData data = MiscData.getInstance();

    private static int      DATABASE_VERSION   = 1;
    private static String   DATABASE_NAME      = "Timetable.db";
    private static  String[] TABLE_DAY          = new String[MiscData.getInstance().getDays()];
    private static  String   COLUMN_SESSION_NUM = "_session";
    private static  String   COLUMN_NAME        = "_name";
    private static  String   COLUMN_LENGTH      = "_length";


    public TableDBHandler(Context context, SQLiteDatabase.CursorFactory factory){

        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase){

        for(int i = 0; i < data.getDays(); i++){

            TABLE_DAY[i] = data.getDay(i + data.getFirstDay());

        }

        for(int i = 0; i < data.getDays(); i++){
            String query = "CREATE TABLE" + TABLE_DAY[i] + "(" + COLUMN_SESSION_NUM +
                           " INTEGER PRIMARY KEY AUTOINCREMENT " + COLUMN_NAME + " TEXT " +
                           COLUMN_LENGTH + " INTEGER " + ");";

            sqLiteDatabase.execSQL(query);
        }
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1){

        sqLiteDatabase.execSQL("DROP TABLE EXISTS" + TABLE_DAY);
        onCreate(sqLiteDatabase);

    }


    public void addEntry(int day, String name, int length){

        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_LENGTH, length);

        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_DAY[day], null, values);
        db.close();

    }


    //get Session length
    public int getSessionLength(int day, int session){

        int            length;
        SQLiteDatabase db    = getReadableDatabase();
        String         query =
                "SELECT" + COLUMN_LENGTH + "FROM" + TABLE_DAY[day] + "WHERE" + COLUMN_SESSION_NUM +
                "=\"" + session + "\";";

        Cursor c = db.rawQuery(query, null);

        length = c.getInt(c.getColumnIndex(COLUMN_LENGTH));

        return length;

    }


    //get Session name
    public String getSessionName(int day, int session){

        String         name;
        SQLiteDatabase db = getReadableDatabase();
        String query = "SELECT" + COLUMN_NAME + "FROM" + TABLE_DAY[day] +
                       "WHERE" + COLUMN_SESSION_NUM + "=\"" + session + "=\";";

        Cursor c = db.rawQuery(query, null);

        name = c.getString(c.getColumnIndex(COLUMN_NAME));

        return name;
    }

}

在这段代码中,我尝试在运行时创建一个初始化的表数组,这可能是错误来自的地方,或者我可能正在努力。

我添加条目的地方:

tableDBHandler.addEntry(page , sessionSpinners[i].toString(), Integer.parseInt(lengthText[i].getText().toString()));

我尝试将COLUMN_LENGTH的数据类型从TEXT更改为INTEGER,但这并不起作用。

任何想法如何解决这个问题?

4 个答案:

答案 0 :(得分:4)

错误发生在表"TABLESunday": syntax error的创建中。 sql代码的语法上缺少空格。试试这个:

String query = "CREATE TABLE " + TABLE_DAY[i] + "(" + COLUMN_SESSION_NUM +
                       " INTEGER PRIMARY KEY AUTOINCREMENT " + COLUMN_NAME + " TEXT " +
                       COLUMN_LENGTH + " INTEGER " + ");";

答案 1 :(得分:0)

在我的情况下,“列名”和“值”的数量不匹配。 例如

insert INTO cars (car_id,Name) values (v1,v2,v3);

列数是2,值是3;

答案 2 :(得分:0)

就我而言,我使用SQLite更新表的名称。 仅此而已!

答案 3 :(得分:0)

我是笨蛋还是我的错,以上给出的解决方案都不适合我,我在 External Storage 上创建数据库并且应用程序没有 android.permission.WRITE_EXTERNAL_STORAGE 权限,通过授予权限解决,谢谢