gpt4 book ai didi

安卓数据库: A few simple questions from a beginner

转载 作者:行者123 更新时间:2023-11-29 13:21:09 24 4
gpt4 key购买 nike

所以基本上在过去的几天里我一直在努力让这一切顺利进行。我以前有过 Android 开发经验,但没有坚持下去。关于我的内容已经足够了,更多关于应用程序的内容。

很简单,它由4个类组成:

Activity 1:数据输入 Activity 2:数据输出(显示 Activity 1中刚刚输入的值)

DatabaseHelper 和 DatabaseManager。

数据库表本身非常简单:

<小时/>

| _id |用户名 |

我认为这些值将进入数据库。但是,当我尝试通过输出下一个 Activity 来确认这一点时,我收到错误。

如果有人能为我澄清一些琐碎的事情,我将非常感激:

  • SQLite 如何处理主键的自动递增?我可以专门向用户名字段添加条目,还是应该找到主键 _id 字段的最后一个值,然后手动递增它?

    • 如何从游标中获取值?我应该包含我要查找的 _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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com