我正在使用Eclipse ADT。我想做一些事情,当我按下此按钮时,用户名,经度,纬度等信息存储在使用SQLite数据库浏览器创建的表中。我从老师那里了解到我必须使用insert sql之类的东西。但我对android很新,不知道完整的代码是什么。有人可以帮我这个吗?
我已经看过像upsert这样的问题但没有插入或更新,但我不知道其中的区别。
答案 0 :(得分:0)
我认为你不必将数据从数据库中的数据复制到新数据库,只需使用以下代码将资产中的数据库复制到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();
}
}