我的数据库中有五个表我想再将两个colomns添加到一个表中 我怎么能提前帮助我。 这是我的表格
sqLiteDatabase.execSQL(CREATE_ProductSales_TABLE);
sqLiteDatabase.execSQL(CREATE_UNIT_TABLE);
sqLiteDatabase.execSQL(CREATE_STOCK_TABLE);
sqLiteDatabase.execSQL(CREATE_OrderSales_TABLE);(i want to add two coloms to this table)
sqLiteDatabase.execSQL(CREATE_Prod_TABLE);
sqLiteDatabase.execSQL(CREATE_CAT_TABLE);
如何执行onupgrade方法 我应该写什么来更新android中的表和非更新表
答案 0 :(得分:0)
创建表后,您可以使用alter语句添加
等列ALTER TABLE {OrderSales_Table} ADD COLUMN ColumnName {Integer};
答案 1 :(得分:0)
使用我用来创建表格的SQLiteOpenHelper
类。
看看我的班级
public class DBHelper extends SQLiteOpenHelper {
// Database Information
public static final String DB_NAME = "ROADTRIP_DB";
// database version
static final int DB_VERSION = 1;
//<editor-fold desc = "TABLE NAMES">
public static final String DETAILS = "details";
//</editor-fold>
//<editor-fold desc="TABLE DETAILS">
public static final String ID = "_id";
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String MOBILE = "mobile";
public static final String DATETIMESTAMP = "datetimestamp";
public static final String STATUS = "status";
//</editor-fold>
//<editor-fold desc="CREATE_ROADTRIP_TABLE">
private static final String CREATE_ROADTRIP_TABLE = "CREATE TABLE IF NOT EXISTS " + DETAILS + "("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ NAME + " VARCHAR,"
+ EMAIL + " VARCHAR,"
+ MOBILE + " VARCHAR,"
+ DATETIMESTAMP + " DATETIME,"
+ STATUS + " INTEGER)";
//</editor-fold>
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_ROADTRIP_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
onCreate(db);
}
}
我还使用另一个类在我的数据库上添加命令
public class DBManager {
private DBHelper DBHelper;
private Context context;
private SQLiteDatabase database;
public DBManager(Context c) {
context = c;
}
public DBManager open() throws SQLException {
DBHelper = new DBHelper(context);
database = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
//inserting data to database
//<editor-fold desc = "insertToDetails">
public void insertToDetails(String name,
String email,
String mobile,
String datetimestamp,
String status) {
ContentValues contentValue = new ContentValues();
contentValue.put(DBHelper.NAME, name);
contentValue.put(DBHelper.EMAIL, email);
contentValue.put(DBHelper.MOBILE, mobile);
contentValue.put(DBHelper.DATETIMESTAMP, datetimestamp);
contentValue.put(DBHelper.STATUS, status);
database.insert(DETAILS, null, contentValue);
}
//</editor-fold>
//for getting the data from database fuction
public Cursor fetchData() {
String[] columns = new String[]{
DBHelper.ID,
DBHelper.NAME,
DBHelper.EMAIL,
DBHelper.MOBILE
};
Cursor cursor = database.query(DETAILS, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
}
当我想向我的database
添加数据时。我打电话
dbManager.insertToDetails("column1","column2"...);
花点时间阅读本文。这真的让你的生活更加轻松database
修改强>
在使用之前,请确保必须声明:
SQLiteDatabase sqldb;
DBHelper dbHelper;
DBManager dbManager;
并在您希望使用数据库防止数据库泄漏时,在onCreate
(for extends AppCompactActivity
)或onCreateView
(Fragment
)上调用此方法。希望这有帮助
dbHelper = new DBHelper(getContext());
sqldb = dbHelper.getReadableDatabase();
dbManager = new DBManager(getActivity());
dbManager.open();
答案 2 :(得分:0)
您可以使用alter语句添加新列,如
sqLiteDatabase.execSQL(“ALTER TABLE Tablename ADD COLUMN Newcolumn VARCHAR DEFAULT 0”);