我在我的 android java 项目中使用了多个 SQLite 数据库。第一个(用于登录)工作正常,使用以下代码,我正在关闭连接。但是,在下一页上,我正在调用一个使用不同数据库的函数,但它似乎仍然引用第一个数据库而不是新数据库。我收到此错误:
09-14 13:18:01.990: I/SqliteDatabaseCpp(10035): sqlite returned: error code = 1, msg = no such table:
NextID, db=/mnt/extSdCard/DirectEnquiries/AuditingData/Static/Users
没错,NextID 不在用户中。
这是使用用户表的登录页面的代码:
if(String.valueOf(loginCount).equals("2")) {
File dbfile = new File(Global.StaticDB + "/Users" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
Cursor c = db.rawQuery("SELECT UserID from Users where UserID like '" + txtUsername.getText().toString().trim() + "' AND Password like '" + txtPassword.getText().toString().trim() + "'", null);
c.moveToFirst();
if(c.getCount() > 0) {
Global.Username = c.getString(c.getColumnIndex("UserID"));
Global.currentDB = spnLocation.getSelectedItem().toString();
Global.currentDBfull = Global.sctArea + Global.currentDB;
db.close();
Context context = getApplicationContext();
CharSequence text = "Logged In";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent ShowMainPage = new Intent(view.getContext(), MainPage.class);
startActivityForResult(ShowMainPage, 0);
}
下一页正在使用这个:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(Global.Username);
TextView dbtxt = (TextView) findViewById(R.id.txtDB);
dbtxt.setText(Functions.getNextID("StationObjects"));
}
功能是:
public static int getNextID(String tablename) {
Log.e("Current DB is: ", Global.currentDBfull);
File dbfile = new File(Global.currentDBfull);
SQLiteDatabase dbF = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
int rtnID=(int)DatabaseUtils.longForQuery(dbF,"Select NxtNumber from NextID where NxtTable like '" + tablename + "'",null);
rtnID += 1;
//db.rawQuery("update NextID set NxtNumber = '" + rtnID + "' where NxtTable like 'StationObjects'", null);
dbF.close();
return rtnID;
}
如何确保它使用第二个数据库而不是第一个?汤姆