错误代码:1(SQLITE_ERROR)原因:SQL(query)错误或数据库丢失

时间:2019-03-08 02:05:25

标签: java android

这是我的数据库助手类,出现错误,这是您的控制台

“原因:SQL(查询)错误或数据库丢失。(无此表:配置文件(代码1):,编译时:从配置文件中选择*,其中id_profile = 6)”   引起原因:SQL(query)错误或数据库丢失。 (无此表:配置文件(代码1):,而在编译时:从id_profile = 6的配置文件中选择*)
  引起原因:SQL(query)错误或数据库丢失。 (无此表:配置文件(代码1):,而在编译时:从id_profile = 6的配置文件中选择*)
  引起原因:SQL(query)错误或数据库丢失。 (无此表:配置文件(代码1):,而在编译时:从id_profile = 6的配置文件中选择*)
  引起原因:SQL(query)错误或数据库丢失。 (无此表:配置文件(代码1):,而在编译时:从id_profile = 6的配置文件中选择*)
  引起原因:SQL(query)错误或数据库丢失。 (没有这样的表:概要文件(代码1):,而在编译时:从id_profile = 6的概要文件中选择*)原因:SQL(query)错误或缺少数据库。 (没有这样的表:概要文件(代码1):,而在编译时:从id_profile = 6的概要文件中选择*)原因:SQL(query)错误或缺少数据库。 (无此表:配置文件(代码1):,而在编译时:从id_profile = 6的配置文件中选择*)
  引起原因:SQL(query)错误或数据库丢失。 (没有这样的表:概要文件(代码1):,而在编译时:从id_profile = 6的概要文件中选择*)原因:SQL(query)错误或缺少数据库。 (没有这样的表:profile(代码1):,而在编译时:从id_profile = 6的配置文件中选择*)

任何人请访问帮助

public class DatabaseHelper extends SQLiteOpenHelper {



    private static String DB_NAME = "ocinator.db";
    private static String DB_PATH = "";
    private static final int DB_VERSION = 8;

    private SQLiteDatabase mDataBase;
    private final Context mContext;
    private boolean mNeedUpdate = false;

static final String PROFILE_TABLE = "CREATE TABLE profile (" +
        "    id_profile      INTEGER      PRIMARY KEY AUTOINCREMENT," +
        "    first_name      VARCHAR (15)," +
        "    last_name       VARCHAR (15)," +
        "    email           VARCHAR (50)," +
        "    phone           VARCHAR (30)," +
        "    login_status    VARCHAR (20)," +
        "    username        VARCHAR (30)," +
        "    password        TEXT," +
        "    ocinator_id     DOUBLE," +
        "    name            VARCHAR (39)," +
        "    nickname        VARCHAR (30)," +
        "    registered_date DATETIME" +
        ");";


    static final String NOTIFICATIONS_TABLE = "CREATE TABLE notification_oc (" +
            "    notification_id           INTEGER  PRIMARY KEY AUTOINCREMENT," +
            "    notification_title        TEXT," +
            "    notification_message      TEXT," +
            "    notification_firetime     DATETIME," +
            "    notification_creationtime DATETIME DEFAULT (CURRENT_TIMESTAMP)," +
            "    notification_status       TEXT" +
            ");" ;



    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        if (android.os.Build.VERSION.SDK_INT >= 17)
            DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
        else
            DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        this.mContext = context;

        copyDataBase();

        this.getReadableDatabase();
    }

    public void updateDataBase() throws IOException {
        if (mNeedUpdate) {
            File dbFile = new File(DB_PATH + DB_NAME);
            if (dbFile.exists())
                dbFile.delete();

            copyDataBase();

            mNeedUpdate = false;
        }
    }

    private boolean checkDataBase() {
        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

    private void copyDataBase() {
        if (!checkDataBase()) {
            this.getReadableDatabase();
            this.close();
            try {
                copyDBFile();
            } catch (IOException mIOException) {
                throw new Error("ErrorCopyingDataBase");
            }
        }
    }



    private void copyDBFile() throws IOException {

        System.out.println("Input xx started");
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        //InputStream mInput = mContext.getResources().openRawResource(R.raw.info);
        OutputStream mOutput = new FileOutputStream(DB_PATH + DB_NAME);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer)) > 0)
            mOutput.write(mBuffer, 0, mLength);
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    public boolean openDataBase() throws SQLException {
        mDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        return mDataBase != null;
    }

    @Override
    public synchronized void close() {
        if (mDataBase != null)
            mDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

     //   db.execSQL(PROFILE_TABLE);
     //   db.execSQL(NOTIFICATIONS_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion)
            mNeedUpdate = true;

       // db.execSQL("DROP TABLE IF EXISTS profile");
       // db.execSQL("DROP TABLE IF EXISTS notification_oc");
       // onCreate(db);

    }




    public boolean insertNotification (String title, String msg, String time, String status) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("notification_title", title);
        contentValues.put("notification_message", msg);
        contentValues.put("notification_firetime", time);
        contentValues.put("notification_status", status);
        db.insert("notification_oc", null, contentValues);
        return true;
    }

    public ArrayList<String> getAllNotifications() {
        ArrayList<String> array_list = new ArrayList<String>();

        //hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from notification_oc", null );
        res.moveToFirst();

        while(res.isAfterLast() == false){
            array_list.add(res.getString(res.getColumnIndex("notification_title")));
            res.moveToNext();
        }
        return array_list;
    }

    public Cursor getNotification(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from notification_oc where notification_id="+id+"", null );
        return res;
    }

    public int numberOfRowsInNotifications(){
        SQLiteDatabase db = this.getReadableDatabase();
        int numRows = (int) DatabaseUtils.queryNumEntries(db, "notification_oc");
        return numRows;
    }


    public Integer deleteNotification (Integer id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("notification_oc",
                "notification_id = ? ",
                new String[] { Integer.toString(id) });
    }




    public boolean insertUser (String username, String password, String nickname, String email) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("username", username);
        contentValues.put("password", password);
        contentValues.put("nickname", nickname);
        contentValues.put("email", email);
        db.insert("profile", null, contentValues);
        return true;
    }


    public Integer deleteUser (Integer id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("profile",
                "id_profile = ? ",
                new String[] { Integer.toString(id) });
    }

    public Cursor getUser(int id) {

        SQLiteDatabase db = this.getReadableDatabase();
        System.out.println("USERNAMEX1 : 111" );
        Cursor res =  db.rawQuery( "select * from profile where id_profile="+id+"", null );
        System.out.println("USERNAMEX2 : 222" );
        return res;
    }

    // Add your public helper methods to access and get content from the database.
    // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
    // to you to create adapters for your views.

}

1 个答案:

答案 0 :(得分:0)

您正在使用它作为数据库目录:

    if (android.os.Build.VERSION.SDK_INT >= 17)
        DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
    else
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";

然后

    DB_PATH + DB_NAME

您应该将其用作复制目的地。

    context.getDatabasePath() + "/" + DB_NAME

似乎问题是您复制到了SQLiteOpenHelper的目录以外的地方。尽管复制后使用了mDatabase来打开文件,但这不是SQLiteOpenHelper所要使用的,而SQLiteOpenHelper会将其自身用于getReadableDatabase和getWritableDatabase,这不是复制的文件,并且没有所需的表。