我的逻辑是:
只需在按钮点击时将数据插入sqlite数据库。我已创建并且工作正常。
我的要求是:
我想再向现有的Sqlite添加一列,所以我手动添加。
我做的是我删除了模拟器中的现有表并运行我的代码,我有查询来创建一个新表。执行代码表没有得到创建
这是我的代码:
此处User_Id是我的新列在sq-lite DB
中MainActivity.java
@Override
public void onClick(View v) {
Flag="I";
if (valid_JobNo != null
&& valid_Status != null
&& valid_Date_Time != null
&& valid_User_Id != null
&&valid_JobNo.length() != 0
&& valid_Status.length() != 0
&& valid_Date_Time.length() != 0
&&valid_User_Id.length() != 0
&& Flag.length()!=0)
{
flagL = "T";
dbHandler.Add_Tracking(new Tracking(valid_JobNo,
valid_Status, valid_Date_Time,valid_User_Id,flagL));
Toast_msg = "Not Connected To Internet.\nData inserted in Sqlite DB successfully";
Show_Toast(Toast_msg);
Reset_Text();
}
else
{
Toast_msg = "Sorry Some Fields are missing.\nPlease Fill up all.";
Show_Toast(Toast_msg);
}
DB Handler .java
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Cargonet";
private static final String TABLE_Tracking = "CargoTracking";
private static final String KEY_ID = "id";
private static final String KEY_JobNo = "JobNo";
private static final String KEY_Status = "Status";
private static final String KEY_User_Id = "User_Id";
private static final String KEY_Flag = "Flag";
private static final String KEY_Date_Time = "Date_Time";
private final ArrayList<Tracking> Tracking_list = new ArrayList<Tracking>();
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_Tracking_TABLE = "CREATE TABLE " + TABLE_Tracking + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_JobNo + " TEXT,"
+ KEY_Status + " TEXT," + KEY_User_Id + " TEXT," + KEY_Date_Time + " TEXT," + KEY_Flag + " TEXT "+ ")";
db.execSQL(CREATE_Tracking_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Tracking);
// Create tables again
onCreate(db);
}
public void Add_Tracking(Tracking Tracking) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_JobNo, Tracking.getJobNo()); // Tracking JobNo
values.put(KEY_Status, Tracking.getStatus());
values.put(KEY_User_Id, Tracking.getUser_Id());
values.put(KEY_Flag, Tracking.getFlag());// Tracking Phone
values.put(KEY_Date_Time, Tracking.getDate_Time());
// Inserting Row
db.insert(TABLE_Tracking, null, values);
db.close(); // Closing database connection
}
Tracking Get_Tracking(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_Tracking, new String[] { KEY_ID,
KEY_JobNo, KEY_Status, KEY_Date_Time ,KEY_User_Id, KEY_Flag}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Tracking Tracking = new Tracking(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),(cursor.getString(5)));
// return Tracking
cursor.close();
db.close();
return Tracking;
}
Tracking Get_TrackingF(String Flag) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_Tracking, new String[] { KEY_ID,
KEY_JobNo, KEY_Status, KEY_Date_Time ,KEY_User_Id, KEY_Flag}, KEY_Flag + "=?",
new String[] { String.valueOf(Flag) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Tracking Tracking = new Tracking(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), (cursor.getString(5)));
// return Tracking
cursor.close();
db.close();
return Tracking;
}
public ArrayList<Tracking> Get_Tracking() {
try {
Tracking_list.clear();
String selectQuery = "SELECT * FROM " + TABLE_Tracking;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Tracking Tracking = new Tracking();
Tracking.setID(Integer.parseInt(cursor.getString(0)));
Tracking.setJobNo(cursor.getString(1));
Tracking.setStatus(cursor.getString(2));
Tracking.setDate_Time(cursor.getString(3));
Tracking.setUser_Id(cursor.getString(4));
Tracking.setFlag(cursor.getString(5));
// Adding Tracking to list
Tracking_list.add(Tracking);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return Tracking_list;
} catch (Exception e) {
// TODO: handle exception
Log.e("all_Tracking", "" + e);
}
return Tracking_list;
}
public int Update_Tracking(Tracking Tracking) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_JobNo, Tracking.getJobNo());
values.put(KEY_Status, Tracking.getStatus());
values.put(KEY_Date_Time, Tracking.getDate_Time());
values.put(KEY_User_Id, Tracking.getUser_Id());
values.put(KEY_Flag, Tracking.getFlag());
// updating row
return db.update(TABLE_Tracking, values, KEY_ID + " = ?",
new String[] { String.valueOf(Tracking.getID()) });
}
public void Delete_Tracking(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_Tracking, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
}
public int Get_Total_Tracking(Tracking Tracking) {
String countQuery = "SELECT * FROM " + TABLE_Tracking;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
Tracking Get_Flag(String flag) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_Tracking, new String[] { KEY_ID,
KEY_JobNo, KEY_Status, KEY_Date_Time ,KEY_User_Id , KEY_Flag}, KEY_Flag + "=?",
new String[] { String.valueOf(flag) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Tracking Tracking = new Tracking(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4) , (cursor.getString(5)));
cursor.close();
db.close();
return Tracking;
}
public Cursor getListItem() {
// TODO Auto-generated method stub
return null;
}
public void addListItem(ArrayList<String> tracking_data) {
// TODO Auto-generated method stub
}
}
Tracking.java
public class Tracking {
// private variables
public int _id;
public String _JobNo;
public String _Status;
public String _Flag;
public String _Date_Time;
public String _User_Id;
public Tracking() {
}
public Tracking(int id, String JobNo, String _Status, String _Date_Time,String _User_Id, String _Flag) {
this._id = id;
this._JobNo = JobNo;
this._Status = _Status;
this._Date_Time = _Date_Time;
this._User_Id = _User_Id;
this._Flag = _Flag;
}
public Tracking(String JobNo, String _Status, String _Date_Time,String _User_Id, String _Flag) {
this._JobNo = JobNo;
this._Status = _Status;
this._Date_Time = _Date_Time;
this._User_Id = _User_Id;
this._Flag = _Flag;
}
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String getJobNo() {
return this._JobNo;
}
public void setJobNo(String JobNo) {
this._JobNo = JobNo;
}
public String getStatus() {
return this._Status;
}
public void setStatus(String Status) {
this._Status = Status;
}
public String getDate_Time() {
return this._Date_Time;
}
public void setDate_Time(String Date_Time) {
this._Date_Time = Date_Time;
}
public String getUser_Id() {
return this._User_Id;
}
public void setUser_Id(String User_Id) {
this._User_Id = User_Id;
}
public String getFlag() {
return this._Flag;
}
public void setFlag(String Flag) {
this._Flag = Flag;
}
}
答案 0 :(得分:0)
我已经浏览了大部分代码,但我认为你真正要问的是&#34;你如何更新数据库?&#34;
首先,您将更改public void onCreate(SQLiteDatabase db)
函数,以使用要添加的新列创建模型数据库结构。然后将DATABASE_VERSION = 1
更改为DATABASE_VERSION = 2
。如果新版本大于当前版本,则再次运行删除表和onCreate
功能。
此外,关于public void Add_Tracking(Tracking Tracking)
的旁注将其更改为public void Add_Tracking(Tracking tracking)
,并将对该变量的所有引用也更改为以小写字母开头。我不确定这是否会导致您的代码出现任何问题,但当局部变量和类具有相同的名称和格式时,它会让人感到困惑。
答案 1 :(得分:0)
不清楚您的问题是什么或您想要选择添加新列的选项,但通常是添加新列您必须检查数据库版本并升级它并添加新列。你可以在onUpgrade():
中完成 @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// If you need to add a new column
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE "+table_name+" ADD COLUMN "+new_column_name + " INTEGER DEFAULT 0");
}
}
而不是整数默认值0,您还可以使用TEXT NOT NULL或任何其他值您想要放入数据库列。