Android中SQLite构造函数参数Context的几点注意事项(2)

SQLiteActivityHelper代码:

package com.avin.Android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

public class SqliteActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        DBHelper helper = new DBHelper(SqliteActivity.this,"sqlite.db");
        Log.d("Avin","this is onCreate in SqliteActivity...the helper is -->"+helper);
        SQLiteDatabase sdb = helper.getReadableDatabase();
       
        Log.d("Avin","this is onCreate in SqliteActivity...the sdb is -->"+sdb);
        sdb = helper.getWritableDatabase();
        //如果不指定主键 会正常运行吗 
        //试试证明,会!
       
        //批量插入
        String[] cands = {"a","b","c","d"};
        String[] keys = {"1","2","3","4"};
       
        for(int i=0; i<cands.length; i++){
         ContentValues cv = new ContentValues();
         cv.put("flt_cand", cands[i]);
         cv.put("flt_key", keys[i]);
         
         sdb.insert("floats", null, cv);
        }
       
       
  Log.d("Avin","insert...");
 
 
 
  sdb.close();
 
  sdb = helper.getReadableDatabase();
 
  Cursor cur = sdb.query("floats", new String[]{"flt_cand"}, "flt_key=?", new String[]{"4"}, null, null, null);
 
  while(cur.moveToNext()){
   String rst = cur.getString(cur.getColumnIndex("flt_cand"));
   Log.d("Avin", rst);
  }
   
    }
}

linux

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/0c32c1b00ed9335ffedb49afdf42422d.html