在 Android studio 中定义 SQLite 数据库

Defining SQLite database in Android studio

有人能告诉我这段代码有什么问题吗?我想简单地定义一个db但是有一个错误我无法掌握解决方案!错误指向这一行 -> hospital_db=SQLiteDatabase.openOrCreateDatabase("mydb",Context.MODE_PRIVATE,null);

package polimi.aap.yas.personalhealthrecord;


    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.ViewGroup;
    import android.content.Intent;
    import android.database.sqlite.SQLiteDatabase;
    import android.widget.EditText;
    import android.widget.Button;

    public class SignUp extends Fragment {

        Intent i=null;
        EditText et1_email,et2_username,et3_password,et4_password2;
        boolean flag=false;
        SQLiteDatabase hospital_db=null;
        Button btn1_signup;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Defines the xml file for the fragment
            View view = inflater.inflate(R.layout.signup, container, false);
            // Setup handles to view objects here
            // etFoo = (EditText) view.findViewById(R.id.etFoo);
            et1_email=(EditText) view.findViewById(R.id.txtEmail_signup);
            et2_username=(EditText) view.findViewById(R.id.txtUsername_signup);
            et3_password=(EditText) view.findViewById(R.id.txtPassword_Signup);
            et4_password2=(EditText) view.findViewById(R.id.txtPassword2_signup);
            btn1_signup=(Button)view.findViewById(R.id.btnRegister_signup);
            hospital_db=SQLiteDatabase.openOrCreateDatabase("mydb", Context.MODE_PRIVATE,null);
            return view;
        }
    }

这是错误:

Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:compileDebugSources, :app:compileDebugAndroidTestSources] :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72311Library UP-TO-DATE :app:prepareComAndroidSupportSupportV42311Library UP-TO-DATE :app:prepareDebugDependencies :app:compileDebugAidl UP-TO-DATE :app:compileDebugRenderscript UP-TO-DATE :app:generateDebugBuildConfig UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets UP-TO-DATE :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources UP-TO-DATE :app:mergeDebugResources UP-TO-DATE :app:processDebugManifest UP-TO-DATE :app:processDebugResources UP-TO-DATE :app:generateDebugSources UP-TO-DATE :app:preDebugAndroidTestBuild UP-TO-DATE :app:prepareDebugAndroidTestDependencies :app:compileDebugAndroidTestAidl UP-TO-DATE :app:processDebugAndroidTestManifest UP-TO-DATE :app:compileDebugAndroidTestRenderscript UP-TO-DATE :app:generateDebugAndroidTestBuildConfig UP-TO-DATE :app:generateDebugAndroidTestAssets UP-TO-DATE :app:mergeDebugAndroidTestAssets UP-TO-DATE :app:generateDebugAndroidTestResValues UP-TO-DATE :app:generateDebugAndroidTestResources UP-TO-DATE :app:mergeDebugAndroidTestResources UP-TO-DATE :app:processDebugAndroidTestResources UP-TO-DATE :app:generateDebugAndroidTestSources UP-TO-DATE :app:compileDebugJavaWithJavac /Users/mrnouri/AndroidStudioProjects/PersonalHealthRecord/app/src/main/java/polimi/aap/yas/personalhealthrecord/SignUp.java Error:(38, 35) error: no suitable method found for openOrCreateDatabase(String,int,) method SQLiteDatabase.openOrCreateDatabase(String,CursorFactory,DatabaseErrorHandler) is not applicable (actual argument int cannot be converted to CursorFactory by method invocation conversion) method SQLiteDatabase.openOrCreateDatabase(String,CursorFactory) is not applicable (actual and formal argument lists differ in length) method SQLiteDatabase.openOrCreateDatabase(File,CursorFactory) is not applicable (actual and formal argument lists differ in length) Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details. Information:BUILD FAILED Information:Total time: 1.163 secs Information:2 errors Information:0 warnings Information:See complete output in console

预先感谢您的帮助。

问题是openOrCreateDatabase()方法的参数是错误的。 SQLiteDatabase class 中此方法的定义如下。

/**
 * Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).
 */
public static SQLiteDatabase openOrCreateDatabase(File file, CursorFactory factory) {
    return openOrCreateDatabase(file.getPath(), factory);
}

/**
 * Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).
 */
public static SQLiteDatabase openOrCreateDatabase(String path, CursorFactory factory) {
    return openDatabase(path, factory, CREATE_IF_NECESSARY, null);
}

/**
 * Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler).
 */
public static SQLiteDatabase openOrCreateDatabase(String path, CursorFactory factory,
        DatabaseErrorHandler errorHandler) {
    return openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler);
}

/**
 * Open the database according to the flags {@link #OPEN_READWRITE}
 * {@link #OPEN_READONLY} {@link #CREATE_IF_NECESSARY} and/or {@link #NO_LOCALIZED_COLLATORS}.
 *
 * <p>Sets the locale of the database to the  the system's current locale.
 * Call {@link #setLocale} if you would like something else.</p>
 *
 * <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be
 * used to handle corruption when sqlite reports database corruption.</p>
 *
 * @param path to database file to open and/or create
 * @param factory an optional factory class that is called to instantiate a
 *            cursor when query is called, or null for default
 * @param flags to control database access mode
 * @param errorHandler the {@link DatabaseErrorHandler} obj to be used to handle corruption
 * when sqlite reports database corruption
 * @return the newly opened database
 * @throws SQLiteException if the database cannot be opened
 */
public static SQLiteDatabase openDatabase(String path, CursorFactory factory, int flags,
        DatabaseErrorHandler errorHandler) {
    SQLiteDatabase db = new SQLiteDatabase(path, flags, factory, errorHandler);
    db.open();
    return db;
}

您可能想在 Context class 中使用相同的方法。它的定义如下:

/**
 * Open a new private SQLiteDatabase associated with this Context's
 * application package.  Create the database file if it doesn't exist.
 *
 * @param name The name (unique in the application package) of the database.
 * @param mode Operating mode.  Use 0 or {@link #MODE_PRIVATE} for the
 *     default operation, {@link #MODE_WORLD_READABLE}
 *     and {@link #MODE_WORLD_WRITEABLE} to control permissions.
 *     Use {@link #MODE_ENABLE_WRITE_AHEAD_LOGGING} to enable write-ahead logging by default.
 * @param factory An optional factory class that is called to instantiate a
 *     cursor when query is called.
 *
 * @return The contents of a newly created database with the given name.
 * @throws android.database.sqlite.SQLiteException if the database file could not be opened.
 *
 * @see #MODE_PRIVATE
 * @see #MODE_WORLD_READABLE
 * @see #MODE_WORLD_WRITEABLE
 * @see #MODE_ENABLE_WRITE_AHEAD_LOGGING
 * @see #deleteDatabase
 */
public abstract SQLiteDatabase openOrCreateDatabase(String name,
        int mode, CursorFactory factory);

所以我认为你可以这样使用:

hospital_db=openOrCreateDatabase("mydb", Context.MODE_PRIVATE,null);