错误:android.database.sqlite.SQLiteException:table用户没有名为name(code 1)的列:,编译时:INSERT INTO用户(名称,余额,密码,年龄)VALUES(?,?,?,? )
<input type="text" id="pesos" name="pesos" value="">
}
public class NewUserTable {
int _id;
String _name;
String _password;
int _balance;
int _age;
public NewUserTable(){
}
public NewUserTable( String name, String password, int balance, int age){
this._name = name;
this._password = password;
this._balance = balance;
this._age = age;
}
public NewUserTable( int id, String name, String password, int balance, int age){
this._id = id;
this._name = name;
this._password = password;
this._balance = balance;
this._age = age;
}
public int getID(){
return this._id;
}
public void setID(int id){
this._id = id;
}
public String getName(){
return this._name;
}
public void setName(String name){
this._name = name;
}
public String getPassword(){
return this._password;
}
public void setPassword(String password){
this._password = password;
}
public int getBalance(){
return _balance;
}
public void setBalance(int balance){
this._balance = balance;
}
public int getAge(){
return _age;
}
public void setAge(int age){
this._age = age;
}
}
public class DatabaseHandler extends SQLiteOpenHelper {
//----------- TABLE COLUMNS -----------//
public static final String KEY_ID = "id"; // eash user has unique id
public static final String KEY_NAME = "name";
public static final String KEY_PASSWORD = "password";
public static final String KEY_BALANCE = "balance";
public static final String KEY_AGE ="age";
//----------- TABLE COLUMNS -----------//
private static final String DATABASE_NAME = "newUser";
private static final String TABLE_NAME = "User";
public DatabaseHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final int DATABASE_VERSION = 1;
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_NEWUSER_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + "INTEGER PRIMARY KEY," + KEY_NAME +
"TEXT," + KEY_PASSWORD + "TEXT," + KEY_BALANCE + "INTEGER,"
+ KEY_AGE + "INTEGER" + ")";
db.execSQL(CREATE_NEWUSER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
public int getUsersCount() {
String countQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// добавить новую запись
public void addUser(NewUserTable newUserTable){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME,newUserTable.getName());
values.put(KEY_PASSWORD,newUserTable.getPassword());
values.put(KEY_BALANCE,newUserTable.getBalance());
values.put(KEY_AGE,newUserTable.getAge());
db.insert(TABLE_NAME, null, values);
db.close();
}
// считать записи
public NewUserTable getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID,
KEY_NAME, KEY_PASSWORD, KEY_BALANCE, KEY_AGE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
NewUserTable newUserTable = new NewUserTable(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2), Integer.parseInt(cursor.getString(3)),
Integer.parseInt(cursor.getString(4)));
// return contact
return newUserTable;
}
} 问题出在哪儿?我怎样才能看到数据库结果?
答案 0 :(得分:1)
更改
String CREATE_NEWUSER_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + "INTEGER PRIMARY KEY," + KEY_NAME +
"TEXT," + KEY_PASSWORD + "TEXT," + KEY_BALANCE + "INTEGER,"
+ KEY_AGE + "INTEGER" + ")";
带
String CREATE_NEWUSER_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
+ KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME +
" TEXT, " + KEY_PASSWORD + " TEXT, " + KEY_BALANCE + " INTEGER, "
+ KEY_AGE + " INTEGER" + ")";
您在创建表脚本中遗漏了一些空格。
答案 1 :(得分:0)
String CREATE_NEWUSER_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + "INTEGER PRIMARY KEY," + KEY_NAME + "TEXT," + KEY_PASSWORD + "TEXT," + KEY_BALANCE + "INTEGER," + KEY_AGE + "INTEGER" + ")";
您需要在列名和列类型之间使用空格。
添加空格后,请卸载您的应用,以便再次调用您的SQLite帮助程序onCreate()
。