Android SQLite插入语句错误
Android SQLite insert statement error
在我的数据库中,我修改了 table。以前它有 2 列,不包括 id。现在它有 3 列,不包括 id。因此,每当我添加一个元组时,它都会显示错误。
为了添加元组,我调用了 addContact 函数
private static final int DATABASE_VERSION = 25;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS1 = "contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
private static final String KEY_UNIQUE = "unique";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS1 + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_UNIQUE + " INTEGER," + KEY_PH_NO + " TEXT " + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS1);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_UNIQUE, contact.getUnique());
values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone
// Inserting Row
//giving error at following line
db.insert(TABLE_CONTACTS1, null, values);
db.close(); // Closing database connection
}
//错误是Error inserting name=21:58 unique=1717 phone_number=null3
android.database.sqlite.SQLiteException: near "unique": syntax error (code 1): , 编译时: INSERT INTO contacts(name,unique,phone_number) VALUES (?,?,?)
UNIQUE
是 SQL 中的关键字,不能用作标识符。
将列名称重命名为 isunique
或类似的名称,然后卸载您的应用,以便修改后的 onCreate()
实际上是 运行。
在我的数据库中,我修改了 table。以前它有 2 列,不包括 id。现在它有 3 列,不包括 id。因此,每当我添加一个元组时,它都会显示错误。 为了添加元组,我调用了 addContact 函数
private static final int DATABASE_VERSION = 25;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS1 = "contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
private static final String KEY_UNIQUE = "unique";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS1 + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_UNIQUE + " INTEGER," + KEY_PH_NO + " TEXT " + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS1);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_UNIQUE, contact.getUnique());
values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone
// Inserting Row
//giving error at following line
db.insert(TABLE_CONTACTS1, null, values);
db.close(); // Closing database connection
}
//错误是Error inserting name=21:58 unique=1717 phone_number=null3 android.database.sqlite.SQLiteException: near "unique": syntax error (code 1): , 编译时: INSERT INTO contacts(name,unique,phone_number) VALUES (?,?,?)
UNIQUE
是 SQL 中的关键字,不能用作标识符。
将列名称重命名为 isunique
或类似的名称,然后卸载您的应用,以便修改后的 onCreate()
实际上是 运行。