java - Android:SQLite 使用了错误的数据库

标签 java android sqlite

我在我的 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;
}

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

最佳答案

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

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 合作伙伴

关于java - Android:SQLite 使用了错误的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12424506/

相关文章:

C#:无法以编程方式填充 DataGridView

java - 如何将curl调用翻译成java

java - 将 JSON 对象转换为 JSON 数组

android - 如何在 Android 应用程序上为 ImageButton 添加效果?

java.lang.IndexOutOfBoundsException : Index: 0, 大小 : 0?

java - SQLite列越界PreparedStatement

java - 使用 Kotlin DSL 配置 Java 规范时出现 Gradle 编译错误

java - 捕获 Exception 类而不是 Exception 子类是不好的做法吗?

android - Settings.Secure.ALLOW_MOCK_LOCATION 始终返回 1,即使模拟设置已关闭

sqlite - 如何在SQLite中为每个组找到最高的公用号码?