我在android sqlite中创建了一个简单的数据库表,其中我最初插入了一些记录,但问题是每次运行应用程序时,都会再次插入整个记录列表。例如,当我第一次运行应用程序数据库表时,我已经在我的表中插入了3条记录:
类别1-主题1-数据 类别2-主题2-数据 类别3-主题3-数据
现在,如果我在不插入任何新记录的情况下运行此应用程序,则数据库表将如下所示: 类别1-主题1-数据 类别2-主题2-数据 类别3-主题3-数据 类别1-主题1-数据 类别2-主题2-数据 类别3-主题3-数据
我该如何防止这种情况?请帮忙,真的很紧急。
这是我的代码:
DataColumns.java:
public class DataColumns {
String KEY_CATEGORY = "_category";
String KEY_TOPIC = "_topic";
String KEY_DATA= "_datatoshow";
public DataColumns() {}
// TODO Auto-generated constructor stub
public DataColumns(String _CAT, String _TOPIC, String _DATA)
{
this.KEY_CATEGORY = _CAT;
this.KEY_TOPIC= _TOPIC;
this.KEY_DATA = _DATA;
}
public DataColumns(String topic, String data)
{
this.KEY_TOPIC = topic;
this.KEY_DATA = data;
}
public String getCATEGORY()
{
return this.KEY_CATEGORY;
}
public void setCATEGORY(String _cat)
{
this.KEY_CATEGORY = _cat;
}
public String getTOPIC()
{
return this.KEY_TOPIC;
}
public void setTOPIC(String topic)
{
this.KEY_TOPIC = topic;
}
public String getDATA()
{
return this.KEY_DATA;
}
public void setDATA(String data)
{
this.KEY_TOPIC = data;
}
}
DataBaseHandler.java:
public class DataBaseHandler extends SQLiteOpenHelper {
public static final String KEY_CATEGORY = "_CATEGORY";
public static final String KEY_TOPIC = "_TOPIC";
public static final String KEY_DATA = "_DATA";
public static final String SQLITE_TABLE = "DB_TABLE";
private static final String DATABASE_NAME = "WMCPROJECT.db";
private static final int DATABASE_VERSION = 1;
public DataBaseHandler(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
db.execSQL("CREATE TABLE " + SQLITE_TABLE + "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CATEGORY + " TEXT NOT NULL, " + KEY_TOPIC + " TEXT NOT NULL," + KEY_DATA + " TEXT NOT NULL);");
Log.d("db", "DATABASE CREATION");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
// Create tables again
onCreate(db);
}
void addRecord( DataColumns col_values)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CATEGORY,col_values.getCATEGORY());
values.put(KEY_TOPIC, col_values.getTOPIC());
values.put(KEY_DATA, col_values.getDATA());
// Inserting Row
db.insertOrThrow(SQLITE_TABLE, null, values);
db.close(); // Closing database connection
}
}
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DataBaseHandler dbhandler = new DataBaseHandler(this);
dbhandler.addRecord(new DataColumns("CAT1","TOPIC 1","data to show");
dbhandler.addRecord(new DataColumns("CAT2","TOPIC 2","data to show");
dbhandler.addRecord(new DataColumns("CAT3","TOPIC 3","data to show");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:3)
尝试这样的事情......
void addRecord( DataColumns col_values)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CATEGORY,col_values.getCATEGORY());
values.put(KEY_TOPIC, col_values.getTOPIC());
values.put(KEY_DATA, col_values.getDATA());
String sqlQuery = "SELECT * FROM " + TABLENAME + " WHERE " + KEY_CATEGORY + " = " + "\""
+ col_values.getCATEGORY() + "\"";
Cursor c = db.rawQuery(sqlQuery, null);
if (c != null && c.getCount() != 0) {
} else {
db.insert(TABLENAME, null, values);
}
}
答案 1 :(得分:0)
只需在onCreate方法中调用addRecord,就可以在onCreate方法中的DatabaseHandler类中调用它,如下所示:
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + SQLITE_TABLE + "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CATEGORY + " TEXT NOT NULL, " + KEY_TOPIC + " TEXT NOT NULL," + KEY_DATA + " TEXT NOT NULL);");
Log.d("db", "DATABASE CREATION");
addRecord(new DataColumns("CAT1","TOPIC 1","data to show");
addRecord(new DataColumns("CAT2","TOPIC 2","data to show");
addRecord(new DataColumns("CAT3","TOPIC 3","data to show");
}
SQLiteOpenHelper中的onCreate方法仅在第一次创建数据库时运行。
答案 2 :(得分:0)
我测试了你的项目,我发现了一个问题。你必须在DataBaseHandler类的onCreate中使用db参数才能第一次添加。 将DataBaseHandler中的onCreate方法更改为:
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + SQLITE_TABLE
+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CATEGORY
+ " TEXT NOT NULL, " + KEY_TOPIC + " TEXT NOT NULL," + KEY_DATA
+ " TEXT NOT NULL);");
Log.d("db", "DATABASE CREATION");
ContentValues cv = new ContentValues();
cv.put(KEY_CATEGORY, "CAT1");
cv.put(KEY_TOPIC, "TOPIC 1");
cv.put(KEY_DATA, "data to show");
db.insert(SQLITE_TABLE, null, cv);
cv = new ContentValues();
cv.put(KEY_CATEGORY, "CAT2");
cv.put(KEY_TOPIC, "TOPIC 2");
cv.put(KEY_DATA, "data to show");
db.insert(SQLITE_TABLE, null, cv);
cv = new ContentValues();
cv.put(KEY_CATEGORY, "CAT3");
cv.put(KEY_TOPIC, "TOPIC 3");
cv.put(KEY_DATA, "data to show");
db.insert(SQLITE_TABLE, null, cv);
}
我已经测试过它并且工作正常。