Я использую Eclipse ADT. Я хочу что-то сделать, когда при нажатии этой кнопки информация, такая как Username, Longitude, Latitude, сохраняется в таблице, созданной с использованием SQLite Database Browser. Я понимаю из своего учителя, что я должен использовать что-то вроде вставки sql или что-то в этом роде. Но я действительно новичок в андроиде и не уверен, что такое полный код. Кто-нибудь может мне помочь?
Я видел такие вопросы, как upsert, а не вставка или обновление, но я не знаю разницы.
Я думаю, вам не нужно копировать данные из базы данных в базу данных в новую БД, просто скопируйте свой db в актив в место БД в android, используя следующий код.
public class MYDB {
Context c;
public DbHelpr dbhlpr;
String DB_NAME="DB.db";
int DB_VERSION=1;
int count,j;
public String DB_PATH;
public MYDB(Context c)
{
// TODO Auto-generated constructor stub
this.c=c;
dbhlpr=new DbHelpr(c);
// dbReadable=dbhlpr.getReadableDatabase();
}
public void close()
{
dbhlpr.close();
}
public class DbHelpr extends SQLiteOpenHelper
{
public DbHelpr(Context c)
{
super(c, DB_NAME,null, DB_VERSION);
DB_PATH = c.getApplicationInfo().dataDir + "/databases/";
createDataBase() ;
}
public void createDataBase()
{
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
copyDatabase();
}
}
//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//system.out.println("DB file: "+DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = c.getAssets().open("DB_NAME.EXTENSION");
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
{
// TODO Auto-generated method stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
}
}
public int readDBTableFieldCount(String table) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbhlpr.getReadableDatabase();
Cursor cr=db.query(table, null, null, null, null, null, null);
int count=cr.getCount();
cr.close();
db.close();
return count;
}
private void insertDataTo_rssFeed(int _id, int title_id, int sub_id,String title, int url)
{
ContentValues cv=new ContentValues();
cv.put("_id",_id);
cv.put("title_id", title_id);
cv.put("sub_id", sub_id);
cv.put("title",title);
cv.put("url",url);
long res=this.writeDBTable(cv, "t_rssfeed");
}
public Cursor readDBTable(String table) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbhlpr.getReadableDatabase();
Cursor cr=db.query(table, null, null, null, null, null, null);
return cr;
}
public Cursor readMatchingRows(String table,String column1,String column2,String value1,int value2) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbhlpr.getReadableDatabase();
Cursor cr=db.rawQuery("select * from " + table + " where " + column1 + "='" + value1+"' and "+column2+ "="+value2 , null);
return cr;
}
public long writeDBTable(ContentValues cv,String table)
{
SQLiteDatabase sdb=dbhlpr.getWritableDatabase();
long res=sdb.insert(table, null, cv);
sdb.close();
return res;
}
public float delete(String table,String colmName,String data) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbhlpr.getWritableDatabase();
String[] arr={data};
colmName=colmName+"=?";
float res=db.delete(table, colmName,arr );
db.close();
return res;
}
public void myDBUpdate(String tableName,String keyField,ContentValues cv, String KeyValue)
{
String[] col={KeyValue};
SQLiteDatabase db=dbhlpr.getWritableDatabase();
keyField=keyField+"=?";
long res=db.update(tableName, cv, keyField, col);
db.close();
}
}