SQL(查询)错误或缺少数据库错误

时间:2019-03-04 16:04:49

标签: android android-sqlite sql-insert delete-row

SQL(查询)错误或数据库丢失。 (没有这样的表:TABLE_NAME(代码1):编译时:SELECT * FROM TABLE_NAME WHERE IDNO = idNo

当我致电insertData()时,应用程序因显示以下错误而崩溃:

    Caused by: android.database.sqlite.SQLiteException: no such table:
 TABLE_NAME (code 1): , while compiling: SELECT * FROM  TABLE_NAME 
 WHERE IDNO = idNo
     #################################################################
     Error Code : 1 (SQLITE_ERROR)
     Caused By : SQL(query) error or missing database.
        (no such table: TABLE_NAME (code 1): , while compiling: SELECT * FROM  TABLE_NAME  WHERE IDNO = idNo)
     #################################################################
         at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
 Method)
         at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1073)
         at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:638)
         at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
         at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
         at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
         at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
         at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1722)
         at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1661)
         at my.utar.phonesecurity.DatabaseHelper.getData(DatabaseHelper.java:91)
         at my.utar.phonesecurity.BaseProfilingActivity.checkDatabase(BaseProfilingActivity.java:266)
         at my.utar.phonesecurity.BaseProfilingActivity.onCreate(BaseProfilingActivity.java:116)
         at android.app.Activity.performCreate(Activity.java:6977)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)

请查看其中的错误。这样它才能正常工作。

package my.utar.phonesecurity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.File;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "data.db";
    private static final String TABLE_NAME = "screenGestures";
    Context context;
    String[] deleteString = {"ID", "USER", "LENGTH", "ABSOLUTELENGTH", "DURATION", "AVGSPEED", "STARTPRESSURE", "ENDPRESSURE", "AVGPRESSURE",
            "STARTSIZE", "ENDSIZE", "AVGSIZE", "STARTX", "STARTY", "ENDX", "ENDY", "DIRECTION", "AREA", "MOVETYPE", "USERID",
            "TRORTST", "CRATEDAT", "MOBILEMODELNO"};

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        this.context = context;
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(IDNO INTEGER PRIMARY KEY AUTOINCREMENT,ID TEXT,USER TEXT, LENGTH TEXT, ABSOLUTELENGTH TEXT, " +
                "DURATION TEXT, AVGSPEED TEXT, STARTPRESSURE TEXT, ENDPRESSURE TEXT, AVGPRESSURE TEXT, STARTSIZE TEXT, ENDSIZE TEXT, " +
                " AVGSIZE TEXT, STARTX TEXT, STARTY TEXT, ENDX TEXT, ENDY TEXT,DIRECTION TEXT, AREA TEXT, MOVETYPE TEXT, USERID TEXT ," +
                "TRORTST TEXT , CRATEDAT TEXT ,MOBILEMODELNO TEXT)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }


    public boolean insertData(String id, String user, String length, String absoluteLength, String duration, String avgSpeed,
                              String startPressure, String endPressure, String avgPressure, String startSize, String endSize,
                              String avgSize, String startX, String startY, String endX, String endY, String direction,
                              String area, String moveType, String userId, String trOrTst, String cratedAt, String mobileModelNo) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("ID", id);
        contentValues.put("USER", user);
        contentValues.put("LENGTH", length);
        contentValues.put("ABSOLUTELENGTH", absoluteLength);
        contentValues.put("DURATION", duration);`4`
        contentValues.put("AVGSPEED", avgSpeed);
        contentValues.put("STARTPRESSURE", startPressure);
        contentValues.put("ENDPRESSURE", endPressure);
        contentValues.put("AVGPRESSURE", avgPressure);
        contentValues.put("STARTSIZE", startSize);
        contentValues.put("ENDSIZE", endSize);
        contentValues.put("AVGSIZE", avgSize);
        contentValues.put("STARTX", startX);
        contentValues.put("STARTY", startY);
        contentValues.put("ENDX", endX);
        contentValues.put("ENDY", endY);
        contentValues.put("DIRECTION", direction);
        contentValues.put("AREA", area);
        contentValues.put("MOVETYPE", moveType);
        contentValues.put("USERID", userId);
        contentValues.put("TRORTST", trOrTst);
        contentValues.put("CREATEDAT", cratedAt);
        contentValues.put("MOBILEMODEL", mobileModelNo);

        if (db.insert(TABLE_NAME, null, contentValues) == -1) {
            return false;
        }
        return true;
    }

    public int deleteData(int idNo) {
        SQLiteDatabase db = this.getWritableDatabase();

        return db.delete(TABLE_NAME, "IDNO = ?", new String[]{String.valueOf(idNo)});
        /*if (result == -1) {
            return false;
        } else {
            return true;
        }*/
    }

    public Cursor getData(int idNo) {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = " SELECT * FROM  TABLE_NAME  WHERE IDNO = idNo";
        /*String query = "SELECT *  FROM " + TABLE_NAME;*/
        Cursor cursor = db.rawQuery(query, null);
        return cursor;
    }

    public long getSize() {
        SQLiteDatabase db = this.getReadableDatabase();
        int size = (int) db.getPageSize();
        long sizee = new File(db.getPath()).length();
        return size;
    }
}

2 个答案:

答案 0 :(得分:2)

在此行中: String query = " SELECT * FROM TABLE_NAME WHERE IDNO = idNo";

您正在查询一个名为TABLE_NAME的表,看起来您实际上是想查询其名称包含在变量TABLE_NAME中的表,所以它应该类似于:

String query = "SELECT * FROM " + TABLE_NAME + " WHERE IDNO = " + idNo;

还请阅读SQL注入漏洞,并使用参数化查询来避免这些漏洞。

答案 1 :(得分:0)

您的问题是,名为 TABLE_NAME 的表不存在,这是因为TABLE_NAME已包含在 "" 中,并且字面意义为< strong> TABLE_NAME ,并且尚未解析为 screenGestures

要解决此问题,您可以编写String query = " SELECT * FROM " + TABLE_NAME + " WHERE IDNO = idNo";

但是,除非需要,否则建议不要使用 rawQuery 方法,而应使用便捷方法 query

因此,您可能希望使用:-

public Cursor getData(int idNo) {
    SQLiteDatabase db = this.getWritableDatabase();
    String whereclause = "IDNO=?";
    String[] whereargs = new String[]{String.valueOf(idNo)};
    return cursor = db.query(TABLE_NAME,null,whereclause,whereargs,null,null,null);
}
  • 便捷方法具有以下优点:-

    • 底层SQL是为您构建的,因此减少了出错的机会
    • 值(通过第4个参数(上面的whereargs)传递)已正确转义(用引号引起来),因此可以防止SQL注入。
  • 请注意,第3个参数和第4个参数密切相关,也就是说,对于每个(传递值的占位符),都应该是第4个参数中的元素(字符串数组) )。

  • 第二个参数(如果为null)等于所有列(SELECT * FROM .....

  • 上面的代码是内部代码,尚未经过测试或运行。因此,它可能包含简单的错误。

您不妨参考SQLiteDatabase - query。请注意,有4种不同的 query 方法。