关于Android数据库

时间:2011-02-23 09:37:14

标签: java android database sqlite

热门只在数据库中创建一次表,并在android中使用SQLite DB多次将数据插入该表... ??

3 个答案:

答案 0 :(得分:2)

网上有很多例子。请参阅这些链接以供参考,

答案 1 :(得分:0)

您应该对SQLiteOpenHelper进行子类化并利用其便捷方法,这将消除使用SQLite的大量复杂性。见Data Storage

查看NotePadProvider示例。

答案 2 :(得分:0)

这是我的活动课

公共类d扩展活动{

/ **首次创建活动时调用。 * /

@覆盖     public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

的setContentView(R.layout.main);

    String name=getIntent().getStringExtra("name");
    String email=getIntent().getStringExtra("email");
    String phone=getIntent().getStringExtra("phone");

    Log.e("d3","came to other activity");
    SmartDBHelper d=new SmartDBHelper(getApplicationContext());
   d.open();
   d.insertTitle(name, phone, email);
  Cursor c=d.getAllTitles();
  // c.getColumnName(0); 

  for( int i=0;i<c.getCount();i++){
  if(c.moveToNext()){
      Log.e("c", ""+ c.getString(0) );    
      Log.e("d", ""+ c.getString(1) );
      Log.e("d4", ""+ c.getString(2) );   


  }
Log.e("r",""+c.getCount());
finish();
  }

}

}

这是我的sqllite类

公共类SmartDBHelper扩展了SQLiteOpenHelper {

private SQLiteDatabase dBH;
private static final String DATABASE_NAME = "yo.db";  
private static final int DATABASE_VERSION = 2;  
private static final String NOTIFY_TABLE_NAME = "user_notify_data"; 
private static final String HR_TABLE_NAME = "user_hr_data";  
private static final String NOTIFY_TABLE_CREATE =  
    "CREATE TABLE " + NOTIFY_TABLE_NAME + " (counter INTEGER PRIMARY KEY, " + 
    "userresponse TEXT, " + 
    "notifytime TEXT);";  


private static final String HR_TABLE_CREATE = 
    "CREATE TABLE " + HR_TABLE_NAME +  
    " (counter INTEGER PRIMARY KEY, " +   
    "hr TEXT, " + 
    "act TEXT, " + 
    "timestamp TEXT);";

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

@Override 
public void onCreate(SQLiteDatabase db) { 
        // TODO Auto-generated method stub     
        Log.e("smartdbhelper", "before creation");    
        db.execSQL(NOTIFY_TABLE_CREATE);      
        Log.e("smartdbhelper", "middle creation"); 
        db.execSQL(HR_TABLE_CREATE);       
        Log.e("smartdbhelper", "after creation");  
        }   

    @Override   
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
        // TODO Auto-generated method stub     
        } 

    public SmartDBHelper open() throws SQLException 
    {
          dBH = getWritableDatabase(); 
         return this;
    }

    public long insertTitle(String isbn, String title, String publisher) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put("hr", isbn);
        initialValues.put("act", title);
        initialValues.put("timestamp", publisher);
        Log.e("insertes","i");

         dBH.insert(HR_TABLE_NAME, null, initialValues);

返回11111111;         }

    public Cursor getAllTitles() 
    {
        return dBH.query(HR_TABLE_NAME, new String[] {
                 "hr","act","timestamp"},null, 
                null, 
                null, 
                null, 
                null);
    }








    //---closes the database---    
    public void close() 
    {
        close();

}

}

/ *以上代码有效且已经过测试* /