3

Ok, so perhaps I'm completely missing something here, but I've just started working with SQL and Android and from what I can see, updates and inserts back to the database need to be handled individually?

I can retrieve a Cursor containing multiple records, but if I wanted to apply an update to each record in the cursor, I can only see how to do this individually, using ContentValues and Update(), and then the cursor requires reloading??

Same with if I wish to create multiple new records, these appear to be required to be inserted individually? Can I not create a List and bulk insert?

To me this seems slow and cumbersome, and there has to be something I'm not yet aware of (searching for answers is failing me... likely due to using incorrect terms being new to android/java)

Here's a very simple example of what I basically what I want to achieve, however there must be a better way to achieve this functionality. (I know there are some other bad practises in here, just creating a quick eample)

public class HelloSQL extends Activity {

    SQLiteDatabase myDB = null;
    String TableName = "myTable";
    ContentValues myVals = new ContentValues();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String Data = "";

        // Create a Database.
        try {
            myDB = this.openOrCreateDatabase("myTestDB", MODE_PRIVATE, null);

            // Create a Table in the Database.
            myDB.execSQL("CREATE TABLE IF NOT EXISTS " + TableName
                    + " ( id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + " Value1 INTEGER, Value2 INTEGER);");

            myDB.execSQL("DELETE FROM " + TableName);

            // insert value1
            for (int i = 0; i < 100; i++) {
                myVals.put("Value1", i);
                myDB.insert(TableName, null, myVals);
            }

            // retrieve data from database
            Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);

            int colId = c.getColumnIndex("id");
            int col1 = c.getColumnIndex("Value1");
            int col2 = c.getColumnIndex("Value2");
            Random generator = new Random();

            // Loop through all values and update value2
            c.moveToFirst();
            if (c != null) {                
                do {                    
                    long id = c.getLong(colId);
                    int val1 = c.getInt(col1);
                    int randomVal = generator.nextInt(99);
                    myVals.put("Value1", val1);
                    myVals.put("Value2", randomVal);
                    myDB.insert(TableName, null, myVals);
                    myDB.update(TableName, myVals, "id=?",new String[] {Long.toString(id)});
                } while (c.moveToNext());
            }

            // requery data
            c = myDB.rawQuery("SELECT * FROM " + TableName, null);

            // display all values
            c.moveToFirst();
            if (c != null) {
                do {
                    int val1 = c.getInt(col1);
                    int val2 = c.getInt(col2);
                    Data = Data + Integer.toString(val1) + " " + Integer.toString(val2)  + "\n";
                } while (c.moveToNext());
            }           


            TextView tv = new TextView(this);
            tv.setText(Data);
            setContentView(tv);

        } catch (Exception e) {
            Log.e("Error", "Error", e);
        } finally {
            if (myDB != null)
                myDB.close();
        }
    }

Thanks in advance, don't expect anyone to re-write everything I've done here, but apoint in the right direction and links to a sample would be greatly appreciated.

4

1 回答 1

2

更新和插入回数据库需要单独处理吗?

INSERT在我能想到的每个 SQL 数据库上,在我能想到的每个平台上,语句“需要单独处理”。任何支持批量插入模式的东西都是 SQL 之上的包装器。例外是INSERT从另一个表的子表中获取其数据SELECT,我认为这不是您所指的。

UPDATE语句将更新您在WHERE子句中指定的任何行,可以是零个、一个或多个。同样,这就是 SQL 的工作方式。

但是如果我想对游标中的每条记录应用更新,我只能看到如何单独执行此操作,使用 ContentValues 和 Update(),然后游标需要重新加载?

如果更改相同,请使用单个UPDATE语句和适当WHERE的子句(通过update()方法,如果适合您的更新模式,或通过execSQL())。您需要 ,requery()因为CursorCursor修改之前保存了上一个查询的结果集。

我不能创建列表和批量插入吗?

欢迎您编写随心所欲的 Java 代码。但是,您的约束是 SQL/SQLite。

于 2010-06-24T10:42:09.653 回答