sqlite 在Android直播间创建表

kx1ctssn  于 2022-11-15  发布在  SQLite
关注(0)|答案(3)|浏览(203)

错误:android.database ase.sqlite.SQLiteException:表USER没有名为NAME(CODE 1):的列,编译时:INSERT INTO USER(NAME,BALANCE,PASSWORD,AGE)值(?,?)

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;
}

}

public class CreateUserActivity extends AppCompatActivity {
EditText etName;
EditText etPassword;
EditText etBalance;
EditText etAge;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_user);
}

public void CreateUser(View view) {
    etName = (EditText)findViewById(R.id.etName);
    etPassword = (EditText)findViewById(R.id.etPassword);
    etBalance = (EditText)findViewById(R.id.etBalance);
    etAge = (EditText)findViewById(R.id.etAge);
    String name = String.valueOf(etName.getText());
    String password = String.valueOf(etPassword.getText());
    int balance = Integer.parseInt(String.valueOf(etBalance.getText()));
    int age = Integer.parseInt(String.valueOf(etAge.getText()));

    //write to database from our edit texts
    DatabaseHandler db = new DatabaseHandler(this);
    Log.d("Insert: ", "Inserting ..");
    db.addUser(new NewUserTable(name,password,balance,age));
}

}问题出在哪里?我如何才能看到数据库结果?

xvw2m8pv

xvw2m8pv1#

变化

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" + ")";

您在CREATE TABLE脚本中遗漏了几个空格。

shstlldc

shstlldc2#

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()

krcsximq

krcsximq3#

如果您在此字符串中修改了某些内容,则需要卸载该应用程序并再次运行,才能应用更改。其他的修改不适用,因为。

相关问题