Android数据库:初学者的一些简单问题

时间:2013-12-29 19:33:29

标签: android mysql database cursor

所以基本上我在过去的几天里一直在拉我的头发试图让这一切顺利。我之前有Android开发经验,但没有持久性。但对我而言,更多关于应用程序。

非常简单,它由4个类组成:

活动1:数据输入 活动2:数据输出(显示在活动1中输入的值)

DatabaseHelper和DatabaseManager。

数据库表本身非常简单:


| _id |用户名|

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

如果有人能为我澄清一些微不足道的东西,我将非常感激:

  • 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.
    }
}

结果输出活动

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

}

1 个答案:

答案 0 :(得分:1)

自动增量无问题。

您的错误来源如下: 要将内容实际插入表中,您必须通过结束连接来提交更改:

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.