2012-09-14 21 views
6

我在我的android java项目中使用了多个SQLite数据库。第一个(用于登录)工作正常,使用下面的代码,我正在关闭连接。但是,在接下来的页面中,我调用了一个使用不同数据库的函数,但它似乎仍然引用第一个数据库,而不是新数据库。我得到这个错误:Android: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; 
} 

如何确保它使用第二个数据库而不是第一个? 汤姆

+1

你在哪里闭上你的光标放在第一个登录的数据库? – jnthnjns

+0

刚刚尝试关闭第一个子上的光标,但仍然出现相同的错误 – TMB87

+3

您没有发布很多logcat。我们可以只检查“当前数据库是:”消息,异常情况等吗?什么是Global.sctArea,如果(c.getCount()> 0)是false,会发生什么?你在隐藏许多信息(我知道这是为了简单,但它没有太大的帮助......) –

回答

2

你可以尝试使用以下格式来打开和关闭数据库:

private static String DB_PATH = "/data/data/PACKAGE_NAME/databases/DATABASE_NAME.db"; 
private static SQLiteDatabase db; 
db = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY); 
// RUN YOUR QUERIES ETC 
db.close() 

为2个的数据库,你需要定义2个DB_PATHs并引用他们那里培训相关的情况。

希望这有助于 大号& l合作伙伴