我已经尽可能地浏览了SO。我希望我的表创建中没有语法错误,如我在类似问题的答案中所提到的那样。
请帮我解决这个问题。
未创建所需的列。这是我得到的信息。
E / SQLiteLog:(1)表ContactDetails没有列名 ContactNumber
E / SQLiteDatabase:插入ContactNumber = 9655279648时出错 ContactName = Ganesh R
android.database.sqlite.SQLiteException:table ContactDetails没有名为ContactNumber(code 1)的列:,编译时:INSERT INTO ContactDetails(ContactNumber,ContactName)VALUES(?,?)
public class ProjectConstants {
public static final String myDbName = "ContactsDatabase.db";
public static final String myTableName = "ContactDetails";
public static final String nameColumn = "ContactName";
public static final String numberColumn = "ContactNumber";
}
我有一个单独的类来存储整个项目中访问的常量值。
public class DBHelper extends SQLiteOpenHelper {
private static final String SQLCreateEntries = "CREATE TABLE " + ProjectConstants.myTableName +
" (" + ProjectConstants.nameColumn + ProjectConstants.numberColumn + " )";
private static final String SQLDeleteEntries =
"DROP TABLE IF EXISTS " + ProjectConstants.myTableName;
public DBHelper(Context context) {
super(context, String.valueOf(name), null, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(SQLCreateEntries);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(SQLDeleteEntries);
onCreate(sqLiteDatabase);
}
public void addContact(String nameLabel, String numberLabel) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ProjectConstants.nameColumn, nameLabel);
contentValues.put(ProjectConstants.numberColumn, numberLabel);
sqLiteDatabase.insert(ProjectConstants.myTableName, null, contentValues);
}
}
答案 0 :(得分:1)
您使用
创建了表格"CREATE TABLE " + ProjectConstants.myTableName +
" (" + ProjectConstants.nameColumn + ProjectConstants.numberColumn + " )"
Create query
的语法是
"CREATE TABLE MYTABLENAME (MYCOLUMNNAME MYCOLUMNTYPE, MYSECONDCOLUMNNAME MYSECONDCOLUMNTYPE,...)"
在您的Create query
中,您错过了types
和commas
,请修复它并且程序可以正常运行:)