我是Android开发的新手,我正在开展一项简单的任务。我有一个数据库类:
public class DBHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "myDB";
// Accounts table name
private static final String TABLE_ACCOUNTS = "Accounts";
// Account Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "NameAndSurname";
private static final String KEY_EMAIL = "Email";
private static final String KEY_PASSWORD = "Password";
private static final String KEY_AGE = "Age";
private static final String KEY_TOD = "TypeOfDiabetes";
private static final String KEY_GENDER = "Gender";
private static final String KEY_HEIGHT = "Height";
private static final String KEY_WEIGHT = "Weight";
public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE " + TABLE_ACCOUNTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT,"
+ KEY_PASSWORD + "TEXT,"
+ KEY_AGE + "INTEGER,"
+ KEY_TOD + "TEXT,"
+ KEY_GENDER + "TEXT,"
+ KEY_HEIGHT + "INTEGER,"
+ KEY_WEIGHT + "INTEGER"
+")";
db.execSQL(CREATE_ACCOUNTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCOUNTS);
// Creating tables again
onCreate(db);
}
public void addAccount(Account account) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID,account.getId());
values.put(KEY_NAME, account.getNameAndSurname());
values.put(KEY_EMAIL, account.getEmail());
values.put(KEY_PASSWORD, account.getPassword());
values.put(KEY_AGE, account.getAge());
values.put(KEY_TOD , account.getTypeOfDiabetes());
values.put(KEY_GENDER , account.getGender());
values.put(KEY_HEIGHT , account.getHeight());
values.put(KEY_WEIGHT , account.getWeight());
// Inserting Row
db.insert(TABLE_ACCOUNTS, null, values);
db.close(); // Closing database connection
}
// Getting one Account
public Account getAccount(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_ACCOUNTS, new String[] { KEY_ID,
KEY_NAME, KEY_EMAIL , KEY_PASSWORD , KEY_AGE, KEY_TOD, KEY_GENDER, KEY_HEIGHT, KEY_WEIGHT }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Account acc = new Account(Integer.parseInt(cursor.getString(0)), //id
cursor.getString(1), //name
cursor.getString(2), //mail
cursor.getString(3), //pass
Integer.parseInt(cursor.getString(4)), //age
cursor.getString(5), //TOD
cursor.getString(6), //gender
Integer.parseInt(cursor.getString(7)), //Height
Integer.parseInt(cursor.getString(8))); //Weight
// return Account
return acc;
}
// Getting All Accounts
public List<Account> getAllAccounts() {
List<Account> accountList = new ArrayList<Account>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ACCOUNTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Account account = new Account();
account.setId(Integer.parseInt(cursor.getString(0)));
account.setNameAndSurname(cursor.getString(1));
account.setEmail(cursor.getString(2));
account.setPassword(cursor.getString(3));
account.setAge(Integer.parseInt(cursor.getString(4)));
account.setTypeOfDiabetes(cursor.getString(5));
account.setGender(cursor.getString(6));
account.setHeight(Integer.parseInt(cursor.getString(7)));
account.setWeight(Integer.parseInt(cursor.getString(8)));
// Adding account to list
accountList.add(account);
} while (cursor.moveToNext());
}
// return account list
return accountList;
}
// Getting accounts Count
public int getAccountsCount() {
String countQuery = "SELECT * FROM " + TABLE_ACCOUNTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// Updating an account
public int updateAccount(Account account) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, account.getNameAndSurname());
values.put(KEY_EMAIL,account.getEmail());
values.put(KEY_PASSWORD,account.getPassword());
values.put(KEY_AGE,account.getAge());
values.put(KEY_TOD,account.getTypeOfDiabetes());
values.put(KEY_GENDER,account.getGender());
values.put(KEY_HEIGHT,account.getHeight());
values.put(KEY_WEIGHT,account.getWeight());
// updating row
return db.update(TABLE_ACCOUNTS, values, KEY_ID + " = ?",
new String[]{String.valueOf(account.getId())});
}
// Deleting an account
public void deleteAccount(Account account) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_ACCOUNTS, KEY_ID + " = ?",
new String[] { String.valueOf(account.getId()) });
db.close();
}
}
在我的主要活动中:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
DBHandler db = new DBHandler(this);
// Inserting Account/Rows
Log.d("Insert: ", "Inserting ..");
db.addAccount(new Account(1, "Admin", "admin@admin.com", "admin", 23, "Type 1","Male", 180, 70));
// Reading all accounts
Log.d("Reading: ", "Reading all accounts..");
List<Account> accounts = db.getAllAccounts();
for (Account account : accounts) {
String log = "Id: " + account.getId() + " ,Name: " + account.getNameAndSurname();
// Writing accounts to log
Log.d("Account: : ", log);
}
TextView tx = (TextView) findViewById(R.id.txt);
tx.setText(db.getAccount(1).getNameAndSurname());
} catch (Exception e) {
String log = e.toString();
Log.d("ERROR: ", e.toString());
}
}
}
我收到错误;
表帐户没有名为Gender
的列插入性别错误=男性TypeOfDiabetes =类型1 id = 1 Email = admin@admin.com NameAndSurname =管理员权重= 70身高= 180年龄= 23密码=管理员
和
没有这样的专栏:密码
答案 0 :(得分:0)
错误: 变量definiton和它们的类型规范之间没有空格。
CREATE_ACCOUNTS_TABLE 错误KEY_GENDER +&#34; TEXT,&#34;。 无错误KEY_GENDER +&#34; TEXT,&#34;
要继续,请卸载应用。因为安装的数据库不符合要求的定义。