【问题标题】:Android: SQLite using wrong databaseAndroid:SQLite 使用错误的数据库
【发布时间】:2012-09-14 12:23:29
【问题描述】:

我在我的 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 不在 Users 中。

这是使用用户表的登录页面的代码:

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;
}

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

【问题讨论】:

  • 您在登录数据库上的哪个位置关闭了光标?
  • 刚刚尝试关闭第一个子上的光标但仍然是同样的错误
  • 你没有发布很多你的 logcat。我们可以只检查“当前数据库为:”消息、异常等吗?还有什么是 Global.sctArea,如果 (c.getCount() > 0) 为 false 会发生什么?你隐藏了很多信息(我知道这是为了简单,但它没有多大帮助......)
  • 您确定 Global.currentDBfull 指向正确的路径吗?非常小心地使用静态变量。您是否尝试过使用 DatabaseUtils.longForQuery 的原始查询插入?
  • 你能把整个日志贴出来吗..它的数据不足

标签: java android sqlite


【解决方案1】:

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

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_PATH 并在相关的地方引用它们。

希望这会有所帮助, L & L 合伙人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2017-04-25
    • 2017-01-29
    • 1970-01-01
    相关资源
    最近更新 更多