我有一个sqlitedatabase,当我插入当前日期时,我在数据库中的先前日期也会更改为当前日期(我不想要)。有人可以查看我的代码并告诉我为什么它会将现有日期覆盖到数据库的当前日期。输入数据库的名称不会被覆盖,只会覆盖日期。感谢
public class MyDatabase {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME ="persons_name";
public static final String KEY_DATE = "current_date";
private static final String DATABASE_NAME = "PersonsDatabase";
private static final String DATABASE_TABLE = "personsTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(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 " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_DATE + " TEXT NOT NULL, " +
KEY_NAME + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public MyDatabase(Context c){
ourContext = c;
}
public MyDatabase open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String currDate, String name){
ContentValues cv = new ContentValues();
cv.put(KEY_DATE, currDate);
cv.put(KEY_NAME , name);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData(){
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID,KEY_DATE,KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result =" ";
int iRow = c.getColumnIndex(KEY_ROWID);
int iDate = c.getColumnIndex(KEY_DATE);
int iName = c.getColumnIndex(KEY_NAME);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iDate) + " " + c.getString(iName) + "\n";
}
return result;
}
//我正在插入另一个活动的数据
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String mydate = sdf.format(new Date(System.currentTimeMillis()));
MyDatabase enterData = new MyDatabase(MainActivity.this);
enterData.open();
enterData.createEntry(mydate, enter_personName);
enterData.close();
答案 0 :(得分:1)
按以下方式更改createEntry()
方法,
public long createEntry(String currDate, String name)
{
SQLiteDatabase db = getWritableDatabase(); // Add this line
ContentValues cv = new ContentValues();
cv.put(KEY_DATE, currDate);
cv.put(KEY_NAME , name);
return db.insert(DATABASE_TABLE, null, cv); // Modify this line
}
答案 1 :(得分:0)
您的数据库类的设计是以不正确的方式完成的,因此我在这里重新设计您的数据库类。 在您的代码中进行更新,如下所示,
MyDatabase.java
public class MyDatabase extends SQLiteOpenHelper
{
private String KEY_ROWID = "_id";
private String KEY_NAME ="persons_name";
private String KEY_DATE = "current_date";
private String DATABASE_NAME = "PersonsDatabase";
private String DATABASE_TABLE = "personsTable";
private int DATABASE_VERSION = 1;
public MyDatabase(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_DATE + " TEXT NOT NULL, " +
KEY_NAME + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion )
{
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public long createEntry(String currDate, String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_DATE, currDate);
cv.put(KEY_NAME , name);
return db.insert(DATABASE_TABLE, null, cv);
}
public String getData()
{
SQLiteDatabase db = this.getWritableDatabase();
String[] columns = new String[]{KEY_ROWID,KEY_DATE,KEY_NAME};
Cursor c = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result =" ";
int iRow = c.getColumnIndex(KEY_ROWID);
int iDate = c.getColumnIndex(KEY_DATE);
int iName = c.getColumnIndex(KEY_NAME);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
result = result + c.getString(iRow) + " " + c.getString(iDate) + " " + c.getString(iName) + "\n";
}
return result;
}
}
要在上面的类中使用插入数据,您可以使用以下语句
private MyDatabase db;
db = new MyDatabase(this); // this refers to Activity
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String mydate = sdf.format(new Date(System.currentTimeMillis()));
db.createEntry(mydate, enter_personName);