2012-07-17 44 views
0

在我的應用程序中,我必須創建一個2列5行的表。現在我需要一次更新5個值。我有1個屏幕5 EditText s和保存Button。點擊保存後,5個編輯框中的值應該在數據庫中更新。可以幫助我的任何人查詢嗎?謝謝提前。在android中的數據庫操作

我的表結構是:

id name 
1  wee 
2  fvfb 
3  erer 
4  fgfg 
5  fggfg 

private static final String DATABASE_CREATE = 

"create table incomexpense(_id integer primary key autoincrement,"+"price text not null,description text not null,"+"quantity text not null,"+"total text not null,"+"category text not null,"+"recurrence text not null,"+"date text not null,"+"groups text not null);"; 


private static final String DATABASE_TABLESPIN = "create table spinner(_id primary key autoincrement,"+" spin text not null);"; 

但只有一個表顯示??幫助我..

+0

ü使用qslite或MySQL的數據庫? – Zamani 2012-07-17 07:33:34

+0

我想你需要一個數組來計算你的數據庫中的行數 – Zamani 2012-07-17 07:35:14

回答

1

這個類將創建一個table..You可以創建必填字段編輯這個類。

public class TableCreater extends SQLiteOpenHelper 
{ 

    public static final String DATABASE_NAME = "userregister.db"; 
    public static final int VERSION = 1; 

    public static final String TABLE_NAME = "USER_SESSION"; 

    public static final String COLUMN_ID = "_id"; 
    public static final String USER_ID = "user_id"; 

    public static final String TABLE_CREATE = 

    "create table " + TABLE_NAME + " (" + COLUMN_ID 
      + " integer primary key autoincrement," + USER_ID + " integer);"; 

    public static final String FIRST_QUERY = "insert into " + TABLE_NAME 
      + " (user_id) values(0);"; 

    public loginchecker(Context context) { 
     super(context, DATABASE_NAME, null, VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(TABLE_CREATE); 
     db.execSQL(FIRST_QUERY); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     Log.w(loginchecker.class.getName(), 
       "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 

    } 

} 

這種方法將更新tables..You數據可以按照您的要求

public InsertingClass InsertID(int uid) 
    { 
     ContentValues values = new ContentValues(); 
     values.put(loginchecker.USER_ID, uid); 
     long insertedId = database.insert(loginchecker.TABLE_NAME, null, 
       values); 
     Cursor cursor = database.query(loginchecker.TABLE_NAME, allColumnsLogin, 
       loginchecker.COLUMN_ID + " = " + insertedId, null, null, 
       null, null); 
     cursor.moveToFirst(); 
     return cursorToModel(cursor); 
    } 
+0

謝謝Androider..My懷疑是我創建了一個數據庫,其中有兩個表,但它只顯示了sqlitebrowser.Y中的一個表正在發生......我將編輯我的帖子與代碼,,請參考.. – 2012-07-17 07:47:14

+0

我希望其中一個表可能不會被創建 – 2012-07-17 07:48:34

+0

我編輯了我的帖子..請引用並告訴我哪裏出錯了? ? – 2012-07-17 07:52:38

0

試試這個:

public boolean updateValue(long rowId, String useridvalue) 
{ 
    ContentValues args = new ContentValues(); 
    args.put(USER_ID, useridvalue); 
    return db.update(TABLE_NAME, args, 
      COLUMN_ID + "=" + rowId, null) > 0; 
}