Sqlite异常,语法错误

时间:2013-03-08 13:49:03

标签: android sqlite syntax-error

我还在学习Android开发,而且我在创建sqlite数据库表时遇到了问题。这是我的代码的一部分:

`

private static final String CREATE_ADS_PICTURE = "CREATE TABLE "
        + AdsBDD.TABLE_ADS_PICTURE + "(" + AdsBDD.ID_PICS
        + " INTEGER PRIMARY KEY AUTOINCREMENT ," + AdsBDD.AD_ID
        + " INTEGER NOT NULL," + AdsBDD.PICTURE + " TEXT," + AdsBDD.FOLDER
        + " TEXT," + AdsBDD.ORDER_NO + " INTEGER NOT NULL);";

private static final String CREATE_AGENCES = "CREATE TABLE "
        + AgencesBDD.TABLE_AGENCES + "(" + AgencesBDD.ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + AgencesBDD.GROUP
        + " INTEGER," + AgencesBDD.USERNAME + " TEXT," + AgencesBDD.EMAIL
        + " TEXT," + AgencesBDD.CONTACT_NAME + " TEXT,"
        + AgencesBDD.REGISTRATION_DATE + " TEXT," + AgencesBDD.ACTIVATION
        + " TEXT," + AgencesBDD.ACTIVE + " INTEGER," + AgencesBDD.STORE
        + " INTEGER," + AgencesBDD.STORE_BANNER + " TEXT,"
        + AgencesBDD.RATING + " REAL," + AgencesBDD.LANGUAGE + " TEXT,"
        + AgencesBDD.IDENTITY + " TEXT," + AgencesBDD.ADDRESS + " TEXT,"
        + AgencesBDD.PHONE + " TEXT," + AgencesBDD.COMPANY + " TEXT,"
        + AgencesBDD.WEBPAGE + " TEXT," + AgencesBDD.CODE_POSTAL + " TEXT,"
        + AgencesBDD.JE_SUIS_UN + " TEXT," + AgencesBDD.VILLE + " TEXT,"
        + AgencesBDD.VIDEO + " TEXT);";

    @Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_ADS_PICTURE);
    db.execSQL(CREATE_AGENCES);
}



   public class AgencesBDD {
public static final String TABLE_AGENCES = "agences";

public static final String ID = "id";
public static final String GROUP = "group";
public static final String USERNAME = "username";
public static final String EMAIL = "email";
public static final String CONTACT_NAME = "contact_name";
public static final String REGISTRATION_DATE = "registration_date";
public static final String ACTIVATION = "activation";
public static final String ACTIVE = "active";
public static final String STORE = "store";
public static final String STORE_BANNER = "store_banner";
public static final String RATING = "rating";
public static final String LANGUAGE = "language";
public static final String IDENTITY = "identity";
public static final String ADDRESS = "address";
public static final String PHONE = "phone";
public static final String COMPANY = "company";
public static final String WEBPAGE = "webpage";
public static final String CODE_POSTAL = "code_postal";
public static final String JE_SUIS_UN = "je_suis_un";
public static final String VILLE = "ville";
public static final String VIDEO = "video";

` 所以问题是Sqlite为表AGENCES抛出了这个异常,但表AD_PICTURE工作正常:

03-08 14:36:01.892: E/AndroidRuntime(10822): FATAL EXCEPTION: main
03-08 14:36:01.892: E/AndroidRuntime(10822): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infonetdiffusion.immohabitation/com.infonetdiffusion.immohabitation.FragmentBaseActivity}: android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: CREATE TABLE agences(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,group INTEGER,username TEXT,email TEXT,contact_name TEXT,registration_date TEXT,activation TEXT,active INTEGER,store INTEGER,store_banner TEXT,rating REAL,language TEXT,identity TEXT,address TEXT,phone TEXT,company TEXT,webpage TEXT,code_postal TEXT,je_suis_un TEXT,ville TEXT,video TEXT);

它应该很容易修复,但我找不到我错了... 任何帮助都会被贬低!

4 个答案:

答案 0 :(得分:6)

您使用sqlite关键字作为列名,即group sqliteKeyword这是限制

答案 1 :(得分:1)

删除NOT NULL列的id

阅读here

答案 2 :(得分:0)

这两种情况并不相同!请注意,第一种情况下的主键没有NOT NULL

答案 3 :(得分:0)

CREATE TABLE agences(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, group INTEGER,username TEXT,email TEXT,contact_name TEXT,registration_date TEXT,activation TEXT,active INTEGER,store INTEGER,store_banner TEXT,rating REAL,语言TEXT,身份TEXT,地址TEXT,手机TEXT,公司TEXT,网页TEXT,code_postal TEXT,je_suis_un TEXT,ville TEXT,video TEXT);

是sql中的一个关键词,你不能将它用作列名,将更改为sql中不能成为关键字的其他字符串