【发布时间】:2014-04-28 12:41:13
【问题描述】:
我已经发布了 android 应用,现在我想更新我的数据库内容,然后在 google play 中发布新版本。
这是我的数据库类的示例:
import java.io.File;
import java.io.FileNotFoundException;
public class DatabaseHandler extends SQLiteOpenHelper{
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 20;
// Database Name
private static final String DATABASE_NAME = "ecovas";
private Context myContext;
//Series Table Name
private static final String TABLE_SERIES = "series";
private static final String TABLE_EPISODE = "episode";
private static final String TABLE_SERVICES = "services";
private static final String TABLE_SENT_SERVICES = "requested_services";
//Series Table Columns names
private static final String KEY_SID = "sid";
private static final String KEY_SNAME = "sname";
private static final String KEY_SIMG = "simg";
private static final String KEY_SSTATUS = "sstatus";
private static final String KEY_STAMP = "sStamp";
//Episdoe Table Columns name
private static final String KEY_EPISODE_ID = "episod_id";
private static final String KEY_EPISODE_NAME = "episode_name";
private static final String KEY_EPISODE_IMG = "episode_img";
private static final String KEY_EPISODE_STATUS = "episode_status";
private static final String KEY_SERIES_ID = "series_id";
private static final String KEY_EPISODE_VIDEO = "episode_video";
private static final String KEY_EPISODE_STAMP = "episode_stamp";
//Services Table Columns name
private static final String KEY_SERVICE = "id";
private static final String KEY_SERVICE_ID = "service_id";
private static final String KEY_SERVICE_NAME = "name";
private static final String KEY_SERVICE_SHORTCODE = "shortcode";
private static final String KEY_SERVICE_COMMAND = "command";
private static final String KEY_SERVICE_MESSAGE_DESCRIPTION = "msg_desc";
private static final String KEY_SERVICE_STAMP = "service_stamp";
private static final String KEY_SERVICE_STATUS = "status";
//requested services table column
private static final String KEY_R_ID = "id";
private static final String KEY_R_SERVICE_ID = "service_r_id";
private static final String KEY_R_OPERATOR_NAME = "op_name";
private static final String KEY_R_STAMP = "r_stamp";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_SERIES_TABLE = "CREATE TABLE " + TABLE_SERIES + "("
+ KEY_SID + " INTEGER PRIMARY KEY," + KEY_SNAME + " TEXT NOT NULL,"
+ KEY_SIMG + " TEXT NOT NULL," + KEY_SSTATUS + " INTEGER NOT NULL, " +
KEY_STAMP + " DATETIME " + ")";
String CREATE_EPISODE_TABLE = "CREATE TABLE " + TABLE_EPISODE + "("
+ KEY_EPISODE_ID + " INTEGER PRIMARY KEY," + KEY_EPISODE_NAME + " TEXT NOT NULL,"
+ KEY_EPISODE_IMG + " TEXT NOT NULL," + KEY_EPISODE_STATUS + " INTEGER NOT NULL, "
+ KEY_SERIES_ID + " TEXT NOT NULL, "
+ KEY_EPISODE_VIDEO + " TEXT NOT NULL, " +
KEY_EPISODE_STAMP + " DATETIME " + ")";
String CREATE_SERVICES_TABLE = "CREATE TABLE " + TABLE_SERVICES + "("
+ KEY_SERVICE + " INTEGER PRIMARY KEY,"
+ KEY_SERVICE_ID + " TEXT NOT NULL , "
+ KEY_SERVICE_NAME + " TEXT NOT NULL, "
+ KEY_SERVICE_SHORTCODE + " TEXT NOT NULL, "
+ KEY_SERVICE_COMMAND + " TEXT NOT NULL, "
+ KEY_SERVICE_MESSAGE_DESCRIPTION + " TEXT NOT NULL, "
+ KEY_SERVICE_STAMP + " TEXT NOT NULL, "
+ KEY_SERVICE_STATUS + " INTEGER NOT NULL DEFAULT 0" + ")";
String CREATE_REQUSTED_SERVICES_TABLE = "CREATE TABLE " + TABLE_SENT_SERVICES + "("
+ KEY_R_ID + " INTEGER PRIMARY KEY, "
+ KEY_R_SERVICE_ID + " INTEGER NOT NULL, "
+ KEY_R_OPERATOR_NAME + " TEXT NOT NULL, "
+ KEY_R_STAMP + " DATETIME " + ")";
db.execSQL(CREATE_SERIES_TABLE);
db.execSQL(CREATE_EPISODE_TABLE);
db.execSQL(CREATE_SERVICES_TABLE);
db.execSQL(CREATE_REQUSTED_SERVICES_TABLE);
}
//getDateTime
public String getDateTime(){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
if(newVersion == 20){
Log.e("Upgrade database worked", "working on");
try{
String destPath = "/data/data/com.ecovas.cartoony/databases";
File f = new File(destPath);
if(!f.exists()){
f.mkdirs();
f.createNewFile();
//copy the db from the assets folder into the database folder
CopyDB(myContext.getAssets().open("ecovas"), new FileOutputStream(destPath + "/ecovas"));
}else{
CopyDB(myContext.getAssets().open("ecovas"), new FileOutputStream(destPath + "/ecovas"));
}
}catch(FileNotFoundException ex){
ex.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
Log.e("newVersion : ", ""+newVersion);
Log.e("oldVersion : ", ""+oldVersion);
if(newVersion > oldVersion){
try{
String destPath = "/data/data/com.ecovas.cartoony/databases";
File f = new File(destPath);
if(!f.exists()){
f.mkdirs();
f.createNewFile();
//copy the db from the assets folder into the database folder
CopyDB(myContext.getAssets().open("ecovas"), new FileOutputStream(destPath + "/ecovas"));
}else{
CopyDB(myContext.getAssets().open("ecovas"), new FileOutputStream(destPath + "/ecovas"));
}
}catch(FileNotFoundException ex){
ex.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
void addSeries(SeriesCls s){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues v = new ContentValues();
v.put(KEY_SNAME, s.getName());
v.put(KEY_SIMG, s.getSIMG());
v.put(KEY_SSTATUS, s.getSStatus());
v.put(KEY_STAMP, getDateTime());
db.insert(TABLE_SERIES, null, v);
}
void addServices(ServicesCls services){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues v = new ContentValues();
v.put(KEY_SERVICE_ID, services.getServiceId());
v.put(KEY_SERVICE_NAME, services.getServiceName());
v.put(KEY_SERVICE_SHORTCODE, services.getShortCode());
v.put(KEY_SERVICE_COMMAND, services.getCommand());
v.put(KEY_SERVICE_MESSAGE_DESCRIPTION, services.getMesg());
v.put(KEY_SERVICE_STAMP, getDateTime());
v.put(KEY_SERVICE_STATUS, services.getStatus());
db.insert(TABLE_SERVICES, null, v);
}
void addRequstedServices(int r_s_id, String op_name){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues v = new ContentValues();
v.put(KEY_R_SERVICE_ID, r_s_id);
v.put(KEY_R_OPERATOR_NAME, op_name);
v.put(KEY_R_STAMP, getDateTime());
db.insert(TABLE_SENT_SERVICES, null, v);
}
void addEpisode(EpisodeCls e){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues v = new ContentValues();
v.put(KEY_EPISODE_NAME, e.getName());
v.put(KEY_EPISODE_IMG, e.getIMG());
v.put(KEY_EPISODE_STATUS, e.getStatus());
v.put(KEY_SERIES_ID, e.getSid());
v.put(KEY_EPISODE_VIDEO, e.getVideo());
v.put(KEY_EPISODE_STAMP, getDateTime());
db.insert(TABLE_EPISODE, null, v);
}
public List<ServicesCls> getAllServices(){
List<ServicesCls> serviceList = new ArrayList<ServicesCls>();
String selectQuery = "SELECT * FROM " + TABLE_SERVICES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
ServicesCls services = new ServicesCls();
services.setId(Integer.parseInt(cursor.getString(0)));
services.setServiceId(cursor.getString(1));
services.setServiceName(cursor.getString(2));
services.setShortCode(cursor.getString(3));
services.setCommand(cursor.getString(4));
services.setMesg(cursor.getString(5));
//services.setDate(cursor.getString(6));
services.setStatus(Integer.parseInt(cursor.getString(7)));
serviceList.add(services);
}while(cursor.moveToNext());
}
cursor.close();
return serviceList;
}
public List<SeriesCls> getAllSS() {
List<SeriesCls> contactList = new ArrayList<SeriesCls>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_SERIES + " ORDER BY sid DESC;";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
SeriesCls s = new SeriesCls();
s.setID(Integer.parseInt(cursor.getString(0)));
s.setName(cursor.getString(1));
s.setSIMG(cursor.getString(2));
s.setSStatus(Integer.parseInt(cursor.getString(3)));
// Adding contact to list
contactList.add(s);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
return contactList;
}
/*List<ServicesCls> getAllServicesOld(){
//SQLiteDatabase db = this.getReadableDatabase();
List<ServicesCls> servicesList = new ArrayList<ServicesCls>();
String selectQuery = "SELECT * FROM " + TABLE_SERVICES + " LIMIT 1";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ServicesCls s = new ServicesCls();
s.setId(Integer.parseInt(cursor.getString(0)));
s.setServiceName(cursor.getString(1));
s.setShortCode(Integer.parseInt(cursor.getString(2)));
s.setCommand(Integer.parseInt(cursor.getString(3)));
s.setMesg(cursor.getString(4));
// Adding contact to list
servicesList.add(s);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
return servicesList;
}*/
List<RequestedServicesCls> getRServices(){
List<RequestedServicesCls> contactList = new ArrayList<RequestedServicesCls>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_SENT_SERVICES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
RequestedServicesCls s = new RequestedServicesCls();
s.setId(Integer.parseInt(cursor.getString(0)));
s.setServiceID(Integer.parseInt(cursor.getString(1)));
s.setOP(cursor.getString(2));
//s.setSStatus(Integer.parseInt(cursor.getString(3)));
// Adding contact to list
contactList.add(s);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
return contactList;
}
//join services table with requested services table
/*List<String> getJoinsServices(){
//Create new querybuilder
SQLiteQueryBuilder _QB = new SQLiteQueryBuilder();
_QB.setTables(TABLE_SERVICES + " INNER JOIN " +
TABLE_SENT_SERVICES + " ON " +
KEY_SERVICE_ID + " = " + KEY_R_SERVICE_ID);
SQLiteDatabase db = this.getReadableDatabase();
List<String> join = new ArrayList<String>();
//String query = "SELECT a.service_id, b.service_id, b.op_name FROM services a INNER JOIN requested_services b ON a.service_id=b.service_id ;";
Cursor cursor = _QB.query(db, null, null, null, null, null, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
RequestedServicesCls s = new RequestedServicesCls();
ServicesCls service = new ServicesCls();
service.setServiceId(cursor.getInt(cursor.getColumnIndex(KEY_SERVICE_ID)));
s.setServiceID(cursor.getInt(cursor.getColumnIndex(KEY_R_SERVICE_ID)));
s.setOP(cursor.getString(cursor.getColumnIndex(KEY_R_OPERATOR_NAME)));
//s.setSStatus(Integer.parseInt(cursor.getString(3)));
String x = String.valueOf(service.getServiceId());
String y = String.valueOf(s.getServiceId());
String z = s.getOP();
// Adding contact to list
//String s_id = String.valueOf(cursor.getInt(cursor.getColumnIndex(KEY_SERVICE_ID)));
//String s_r_id = String.valueOf(cursor.getInt(cursor.getColumnIndex(KEY_R_SERVICE_ID)));
//String s_r_op = cursor.getString(cursor.getColumnIndex(KEY_R_OPERATOR_NAME));
join.add(x);
join.add(y);
join.add(z);
} while (cursor.moveToNext());
}else{
join.add(null);
}
// return contact list
cursor.close();
return join;
}*/
/*List<ServicesCls> getServiceLimit(int id){
SQLiteDatabase db = this.getReadableDatabase();
List<ServicesCls> episodeList = new ArrayList<ServicesCls>();
Cursor cursor = db.query(false, TABLE_SERVICES, new String[] {KEY_SERVICE ,KEY_SERVICE_ID,
KEY_SERVICE_NAME, KEY_SERVICE_SHORTCODE, KEY_SERVICE_COMMAND, KEY_SERVICE_MESSAGE_DESCRIPTION},
KEY_SERVICE + " =? ",
new String[] { String.valueOf(id) }, null,null, null, "1");
if (cursor.moveToFirst()){
do{
ServicesCls s = new ServicesCls();
s.setId(Integer.parseInt(cursor.getString(0)));
s.setServiceId(Integer.parseInt(cursor.getString(1)));
s.setServiceName(cursor.getString(2));
s.setShortCode(Integer.parseInt(cursor.getString(3)));
s.setCommand(Integer.parseInt(cursor.getString(4)));
s.setMesg(cursor.getString(5));
// Adding contact to list
episodeList.add(s);
}while(cursor.moveToNext());
}
cursor.close();
return episodeList;
}*/
List<EpisodeCls> getE(Object id) {
SQLiteDatabase db = this.getReadableDatabase();
List<EpisodeCls> episodeList = new ArrayList<EpisodeCls>();
Cursor cursor = db.query(TABLE_EPISODE, new String[] { KEY_EPISODE_ID,
KEY_EPISODE_NAME, KEY_EPISODE_IMG, KEY_EPISODE_STATUS, KEY_SERIES_ID, KEY_EPISODE_VIDEO },
KEY_SERIES_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor.moveToFirst()){
do{
EpisodeCls e = new EpisodeCls();
e.setId(Integer.parseInt(cursor.getString(0)));
e.setName(cursor.getString(1));
e.setIMG(cursor.getString(2));
e.setStatus(Integer.parseInt(cursor.getString(3)));
e.setSid(Integer.parseInt(cursor.getString(4)));
e.setVideo(cursor.getString(5));
// Adding contact to list
episodeList.add(e);
}while(cursor.moveToNext());
}
cursor.close();
return episodeList;
}
public List<EpisodeCls> getAllEE(Object id) {
List<EpisodeCls> epoList = new ArrayList<EpisodeCls>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EPISODE
+ " WHERE " + KEY_SERIES_ID + " = " + String.valueOf(id);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
EpisodeCls e = new EpisodeCls();
e.setId(Integer.parseInt(cursor.getString(0)));
e.setName(cursor.getString(1));
e.setIMG(cursor.getString(2));
e.setStatus(Integer.parseInt(cursor.getString(3)));
e.setSid(Integer.parseInt(cursor.getString(4)));
e.setVideo(cursor.getString(5));
// Adding contact to list
epoList.add(e);
// Adding contact to list
epoList.add(e);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
return epoList;
}
public void deleteSS(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SERIES, KEY_SID + " = ?",
new String[] { String.valueOf(id) });
db.close();
}
public void deleteE(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EPISODE, KEY_EPISODE_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
}
public void deleteAllServices(){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SERVICES, null, null);
db.close();
}
public int getSeriesVount(){
String countQuery = "SELECT * FROM " + TABLE_SERIES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
//cursor.close();
// return count
return cursor.getCount();
}
public int getEpisodeCount(){
String countQuery = "SELECT * FROM " + TABLE_EPISODE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
public boolean isMatch(String serviceId){
//boolean value = false;
SQLiteDatabase db = this.getReadableDatabase();
//Cursor c = db.query(TABLE_SERVICES, new String[] {KEY_SERVICE, KEY_SERVICE_ID},
// KEY_SERVICE + "=?", new String[]{String.valueOf(serviceId)}, null, null,null);
Cursor c = db.query(false, TABLE_SERVICES, new String[] {KEY_SERVICE ,KEY_SERVICE_ID,
KEY_SERVICE_NAME, KEY_SERVICE_SHORTCODE, KEY_SERVICE_COMMAND, KEY_SERVICE_MESSAGE_DESCRIPTION},
KEY_SERVICE_ID + " =? ",
new String[] { String.valueOf(serviceId) }, null,null, null, null);
if(c.getCount() > 0){
return true;
}else{
return false;
}
//if(c.moveToFirst()){
//while(c.moveToNext()){
//value=true;
//}
//}else{
//value=false;
//}
//Log.e("Matched Service is ", service_id);
//return value;
}
public String getVideo(Object id){
String Video_id;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EPISODE, new String[] { KEY_EPISODE_ID,
KEY_EPISODE_NAME, KEY_EPISODE_IMG, KEY_EPISODE_STATUS, KEY_SERIES_ID, KEY_EPISODE_VIDEO },
KEY_EPISODE_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
//String q = "SELECT " + KEY_EPISODE_VIDEO + " FROM " + TABLE_EPISODE + " WHERE " + KEY_EPISODE_VIDEO + " = "
//+ String.valueOf(id);
//Cursor c = db.rawQuery(q, null);
if(cursor != null)
cursor.moveToFirst();
EpisodeCls e = new EpisodeCls();
e.setVideo(cursor.getString(5));
Video_id = e.getVideo();
Log.e("Video Id : ", Video_id);
return Video_id;
}
private void CopyDB(InputStream is, OutputStream os) throws IOException{
//copy 1K bytes at a time
byte[] buffer = new byte[1024];
int length;
while((length = is.read(buffer)) > 0){
os.write(buffer, 0, length);
}
is.close();
os.close();
}
}
【问题讨论】:
-
问题是什么?
-
您已经实现了
onUpgrade方法。有什么问题? -
当我在google play上传新版本时,app没有更新内容。
-
检查我的答案,并确保您做对了这些事情。现在,一旦发布,您需要给商店一些时间,2-3 小时,然后再次检查应用是否有更新
标签: android database performance sqlite