【问题标题】:Android SQLite Exception Syntax Error (Code 1)Android SQLite 异常语法错误(代码1)
【发布时间】:2015-04-27 13:47:32
【问题描述】:

我正在使用 SQLiteHelper 创建一个数据库,当应用程序尝试运行时,我不断收到异常。

这是我收到的错误消息: 引起:android.database.sqlite.SQLiteException:在“Trip”附近:语法错误(代码 1):,编译时:创建表 Business Trip Expenditure(desc text 主键,餐饮整数 NOT NULL , 饮料整数 NOT NULL , 旅行整数 NOT NULL , 住宿整数 NOT NULL , 其他整数 NOT NULL , Receipt_Retained? real NOT NULL , Created_at text NOT NULL );

public class MySQLiteHelper extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "Business Trip Expenditure";
    //Column Names
    public static final String COLUMN_NAME = "desc";
    public static final String MEAL_COST ="Meals";
    public static final String DRINK_COST = "Drinks";
    public static final String TRAVEL_COST = "Travel";
    public static final String ACCO_COST = "Accommodation";
    public static final String OTHER_COST = "Other";
    public static final String HAS_RECEIPT = "Receipt_Retained?";
    public static final String CREATED_AT = "Created_at";

    private static final String DATABASE_NAME = "task.db";
    private static final int DATABASE_VERSION = 1;

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table "
            + TABLE_NAME + "( " + COLUMN_NAME + " text primary key , " + MEAL_COST + " integer NOT NULL , " + DRINK_COST + " integer NOT NULL , " + TRAVEL_COST + " integer NOT NULL , " + ACCO_COST + " integer NOT NULL , " + OTHER_COST + " integer NOT NULL , " + HAS_RECEIPT + " real NOT NULL , " + CREATED_AT + " text NOT NULL );";

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

如果有人可以帮助我了解创建表的语法,我将不胜感激,我知道这种形式的代码在语法方面很挑剔。

【问题讨论】:

  • 表名之间不能有space。将 ot 替换为 _ 之类的字符。
  • @Prera​​kSola 其实你可以 (public static final String TABLE_NAME = "[Business Trip Expenditure]";)。但不鼓励这样做。
  • 感谢他更新@DerGolem。我不知道。

标签: android sqlite exception syntax


【解决方案1】:

改变:

public static final String TABLE_NAME = "Business Trip Expenditure";

public static final String TABLE_NAME = "Business_Trip_Expenditure";

【讨论】:

  • 或...public static final String TABLE_NAME = "[Business Trip Expenditure]";(也有效,但不鼓励)。
  • 非常感谢哈哈,我现在有一些主要的隧道视野。不知道我怎么没看到,谢谢你的帮助。
猜你喜欢
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 2021-11-30
  • 2019-05-02
相关资源
最近更新 更多