作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以基本上在过去的几天里我一直在努力让这一切顺利进行。我以前有过 Android 开发经验,但没有坚持下去。关于我的内容已经足够了,更多关于应用程序的内容。
很简单,它由4个类组成:
Activity 1:数据输入 Activity 2:数据输出(显示 Activity 1中刚刚输入的值)
DatabaseHelper 和 DatabaseManager。
数据库表本身非常简单:
<小时/>我认为这些值将进入数据库。但是,当我尝试通过输出下一个 Activity 来确认这一点时,我收到错误。
如果有人能为我澄清一些琐碎的事情,我将非常感激:
SQLite 如何处理主键的自动递增?我可以专门向用户名字段添加条目,还是应该找到主键 _id 字段的最后一个值,然后手动递增它?
这是我的代码来补充我的问题
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "NutrtionIntuition";
private static final int DATABASE_VERSION = 2;
private static final String USER_TABLE = "UserTable";
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME = "username";
// Database creation sql statement
public static final String DATABASE_CREATE = "CREATE TABLE " + USER_TABLE + "("
+ KEY_ROWID + " INTEGER PRIMARY KEY," + KEY_USERNAME + " TEXT" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Method is called during creation of the database. Method is also called on upgrade of database?
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
// Method is called during an upgrade of the database,
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
database.execSQL("DROP TABLE IF EXISTS UserTable");
onCreate(database);
}
}
public class DatabaseManager{
private DatabaseHelper dbHelper;
private SQLiteDatabase database;
private static final String USER_TABLE = "UserTable";
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME = "username";
public DatabaseManager(Context context){
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
}
//Insert Records into the User Table
public long insertRecords(int id, String name){
ContentValues values = new ContentValues();
//values.put(KEY_ROWID , id);
Log.v("DEBUG","Inserting value....");
values.put(KEY_USERNAME, name);
return database.insert(USER_TABLE, null, values);
}
public Cursor selectRecords() {
String[] cols = new String[] {KEY_ROWID, KEY_USERNAME};
Cursor mCursor = database.query(true, USER_TABLE, cols, null
, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
Log.v("DEBUG","Returning cursor");
return mCursor; // iterate to get each value.
}
}
结果输出 Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
db = new DatabaseManager(this);
Cursor res = db.selectRecords();
Log.v("DEBUG","Cursor Returned");
String name = res.getString(res.getColumnIndex("username"));
result.findViewById(R.id.textView1);
result.setText(name);
}
最佳答案
自动增量工作没有问题。
您的错误来源如下:要实际将内容插入表中,您必须通过结束连接来提交更改:
database.setTransactionSuccessful();
database.endTransaction();
要获取所有返回的内容,您必须使用
res.moveToFirst();
do {
//fetch _id
int id = res.getInt(0);
//fetch username
String name = res.getString(0);
// do the stuff you want to do with the results here
} while (!res.isLast());
res.close(); // IMPORTANT, crash if not used.
关于安卓数据库: A few simple questions from a beginner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829266/
我是一名优秀的程序员,十分优秀!