我的代码是创建sqlite一个sqlite表,在点击添加按钮时保存输入数据,我想要做的是每次点击按钮NewTab
都会创建一个新表,我搜索了无处不在,我尝试共享偏好也使表格的不同名称没有帮助我,任何人都可以帮助PLZ并感谢你。
public class MyDBHandler extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "Shopping.db";
public static final String TABLE_NAME = "Main_table";
public static final String COL_2 = "NAME";
public static final String COL_3 = "PRICE";
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PRICE TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,String price) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,price);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
}
MainActivty
btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDBHandler myDb = new MyDBHandler(MainActivity.this);
boolean isInserted = myDb.insertData(editName.getText().toString(), null);
if (isInserted == true) {
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
Cursor res = myDb.getAllData();
while (res.moveToNext()) {
final TextView textView = new TextView(MainActivity.this);
String valor1 = "<p style='font-family:verdana'>" + a + "</p> ";
textView.setText(Html.fromHtml(valor1));
lin.addView(textView);
}
} else {
Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
}
}
);
答案 0 :(得分:0)
创建一个你通过sharedPreferences
保存的变量,这是你的table count
然后创建一个sql数据库,然后再做
SQLDATABASENAME.execSql("CREATE TABLE Table" + your_counter_variable_ + " #create rows here ;");
答案 1 :(得分:0)
您还可以使用实际时间获取所有表格的唯一ID