Hot To Create A Table in database only once开发者_如何学编程 and insert data into that table multiple times in android using SQLite DB...??
You should be sub-classing SQLiteOpenHelper and making use of its convenience methods which will remove a lot of the complexity of using SQLite. See Data Storage
Take a look at the NotePadProvider sample.
This is my activity class
public class d extends Activity {
/** Called when the activity is first created. */
@Override
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();
}
}
}
here is my sqllite class
public class SmartDBHelper extends 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);
return 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();
}
}
The above code works and has been tested
精彩评论