android.database.sqlite.SQLiteException:在“ 1”附近:创建新表以存储数据时的语法错误(代码1)

时间:2018-12-26 07:10:49

标签: android sqlite android-studio android-sqlite

package com.example.android.minerals;

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.android.minerals.QuizContract.*;

import java.util.ArrayList;
import java.util.List;

public class QuizDbHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "minerals.db";
    private static final int DATABASE_VERSION = 2;
    //private static final SQLiteDatabase.CursorFactory factory = null;

    private SQLiteDatabase db;
    public QuizDbHelper(Context context) {
        super( context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        this.db = db;

        final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
                QuestionsTable.TABLE_NAME + " ( "+
                QuestionsTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
                QuestionsTable.COLUMN_QUESTION + " TEXT, " +
                QuestionsTable.COLUMN_OPTION1 + " TEXT, " +
                QuestionsTable.COLUMN_OPTION2 + " TEXT, " +
                QuestionsTable.COLUMN_OPTION3 + " TEXT, " +
                ")";


        db.execSQL( SQL_CREATE_QUESTIONS_TABLE );
        fillQuestionsTable();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL( "DROP TABLE IF EXISTS " + QuestionsTable.TABLE_NAME);
        onCreate(db);

    }

}

我出错了。

  

原因:android.database.sqlite.SQLiteException:接近“ 1”:语法   错误(代码1):,编译时:CREATE TABLE quiz_questions(_id   整数主键自动增加,问题文本,选项1文本,   选项2 TEXT,选项3 TEXT,)

当我尝试创建问题表时出现语法错误(代码1)。

4 个答案:

答案 0 :(得分:1)

Caused by: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling:
  

SQLite异常,指示SQL解析存在错误   或执行。

您应从语句中删除最后一个逗号,“ +                 “)”;

尝试

private static final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE IF NOT EXISTS "
        + QuestionsTable.TABLE_NAME + "(" + QuestionsTable._ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + QuestionsTable.COLUMN_QUESTION + " TEXT NOT NULL, "
        + QuestionsTable.COLUMN_OPTION1 + " TEXT NOT NULL, "
        + QuestionsTable.COLUMN_OPTION2 + " TEXT NOT NULL, "
        + QuestionsTable.COLUMN_OPTION3 + " TEXT NOT NULL )";

然后 Clean-Rebuild-Run

答案 1 :(得分:0)

final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
                QuestionsTable.TABLE_NAME + " ( "+
                QuestionsTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
                QuestionsTable.COLUMN_QUESTION + " TEXT, " +
                QuestionsTable.COLUMN_OPTION1 + " TEXT, " +
                QuestionsTable.COLUMN_OPTION2 + " TEXT, " +
                QuestionsTable.COLUMN_OPTION3 + " TEXT, " +
                ")";

将此更改为:

final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
                QuestionsTable.TABLE_NAME + " ( "+
                QuestionsTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
                QuestionsTable.COLUMN_QUESTION + " TEXT, " +
                QuestionsTable.COLUMN_OPTION1 + " TEXT, " +
                QuestionsTable.COLUMN_OPTION2 + " TEXT, " +
                QuestionsTable.COLUMN_OPTION3 + " TEXT " +
                ")";

在最后一个列名称(TEXT)之后删除逗号                  QuestionsTable.COLUMN_OPTION3 +“ TEXT” +                 “)”;

答案 2 :(得分:0)

请勿在列名中使用空格。

错误: 引起原因:android.database.sqlite.SQLiteException:靠近“ 1”:语法错误(代码1):,而在编译时:CREATE TABLE quiz_questions(_id INTEGER PRIMARY KEY AUTOINCREMENT,问题TEXT,选项1 TEXT,选项2 TEXT,选项3 TEXT,)

解决方案: 从列名中删除空格。

使用选项_1,选项_2,选项_3代替选项1,选项2,选项3。

并从查询中删除最后一个逗号。

您的查询应如下所示,

CREATE TABLE quiz_questions ( _id INTEGER PRIMARY KEY AUTOINCREMENT, question TEXT, option_1 TEXT, option_2 TEXT, option_3 TEXT)

答案 3 :(得分:0)

最后一列定义末尾的逗号必须删除。

但是由于您的列名中带有空格option 1,所以会生成您发布的实际错误。解析器认为列名称为option,并且不知道如何处理1。我强烈建议不要将列名(或任何对象的名称)与空格(或字母数字和下划线以外的任何其他字符)一起使用,否则只会造成麻烦。但是,如果您坚持使用此类名称,则必须将其用双引号引起来。

...
final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
        "\"" + QuestionsTable.TABLE_NAME + "\" ( "+
        "\"" + QuestionsTable._ID + "\" INTEGER PRIMARY KEY AUTOINCREMENT " +
        "\"" + QuestionsTable.COLUMN_QUESTION + "\" TEXT, " +
        "\"" + QuestionsTable.COLUMN_OPTION1 + "\" TEXT, " +
        "\"" + QuestionsTable.COLUMN_OPTION2 + "\" TEXT, " +
        "\"" + QuestionsTable.COLUMN_OPTION3 + "\" TEXT " +
        ")";
...

但是请注意,如果这样做,则必须在以后使用的 any 查询中将对象名称括在双引号中。