Nic*_*ick 1 database sqlite android cursor
我正在尝试使用SQLite数据库为我的应用程序存储一些数据.我在我的DbHelper类(扩展了SqLiteOpenHelper)中得到了这个
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ jarsTable + "( "+colID+" integer primary key autoincrement, "+colName+" varchar(100), " +
colGoal+" real not null, "+colBal+" real not null, "+colCurr+" varchar(100));";
public DbHelper(Context context) {
super(context, dbName, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
Run Code Online (Sandbox Code Playgroud)
我在我的DataSource类中有这个
private Jar cursorToJar(Cursor cursor) {
Jar myJar = new Jar();
myJar.setId(cursor.getLong(0));
myJar.setName(cursor.getString(1));
myJar.setBalance(cursor.getDouble(2));
myJar.setGoal(cursor.getDouble(3));
myJar.setCurrency(cursor.getString(4));
return myJar;
}
Run Code Online (Sandbox Code Playgroud)
当我到达上面的myJar.setName行时,我收到问题标题中提到的错误,该错误导致应用程序崩溃.我真的有点困惑,我出错了,我得到一个字符串,我的表在第1列存储一个字段,我想,所以..是的.我在这里先向您的帮助表示感谢
编辑:这是我的人口方法:
public Jar createJar(String name, double goal, String currency) {
ContentValues values = new ContentValues();
values.put(DbHelper.colName, name);
values.put(DbHelper.colGoal, goal);
values.put(DbHelper.colCurr, currency);
values.put(DbHelper.colBal, 0.0);
long insertId = database.insert(DbHelper.jarsTable, null,
values);
Log.d("Insert ID:", ""+insertId);
// To show how to query
Cursor cursor = database.query(DbHelper.jarsTable,
allColumns, DbHelper.colID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
return cursorToJar(cursor);
}
Run Code Online (Sandbox Code Playgroud)
始终使用光标的getColumnIndex()方法来访问列.像这样:
myJar.setName(cursor.getString(cursor.getColumnIndex(colName)));
Run Code Online (Sandbox Code Playgroud)
不是说这绝对是问题 - 因为你还没有展示出人们如何Cursor填充 - 但它很可能.
| 归档时间: |
|
| 查看次数: |
2898 次 |
| 最近记录: |