SQLite插入错误

时间:2016-03-11 21:05:04

标签: android sqlite insert android-sqlite

我在使用SQLite插件时遇到了一些问题 基本上我的应用程序从用户获取URL,下载图像 我想保存图像的名称,文件路径,修改日期和大小 但是我在插入时遇到错误

这是我的图像表类

public final class ImagesTable {
    public static final String DBNAME= " imagesdb.sqlite";
    public static final String TABLE_NAME = "images";
    public static final int VERSION=1;


    public static class ImagesColumns implements BaseColumns {
        public static final String NAME = "name";
        public static final String FILEPATH = "File path";
        public static final String MODIFICATION_DATE= "Date";
        public static final String SIZE="Size";

    }


}

这是SQLiteOpenHelper类

public class sqlOpenHelper extends SQLiteOpenHelper{







    public sqlOpenHelper(Context context){
        super(context, ImagesTable.DBNAME , null, ImagesTable.VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createDatabase(db);

    }
    private  void createDatabase(SQLiteDatabase db){
        db.execSQL("create table "+ ImagesTable.TABLE_NAME + "(" +  ImagesTable.ImagesColumns.NAME + " text ," + ImagesTable.ImagesColumns.FILEPATH + " text ," +
                ImagesTable.ImagesColumns.MODIFICATION_DATE+ " text, " + ImagesTable.ImagesColumns.SIZE+" INTEGER "+ " )");

    }

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

在下载方法中我想将其插入到DB中 这是一段代码:|

ContentValues insertValues = new ContentValues();
                insertValues.put(ImagesTable.ImagesColumns.NAME, filename);
                insertValues.put(ImagesTable.ImagesColumns.FILEPATH, outputFile.getAbsolutePath());
                insertValues.put(ImagesTable.ImagesColumns.MODIFICATION_DATE, outputFile.lastModified());
                insertValues.put(ImagesTable.ImagesColumns.SIZE, outputFile.getTotalSpace());
                db.insert(ImagesTable.TABLE_NAME, null, insertValues);

我得错误

03-11 21:54:21.992 9948-11118/com.example.lukasz.myapplication2345 E/SQLiteLog: (1) near "path": syntax error
03-11 21:54:22.017 9948-11118/com.example.lukasz.myapplication2345 E/SQLiteDatabase: Error inserting File path=/data/data/com.example.lukasz.myapplication2345/files/6007405458_0b254ea768_b.jpg name=6007405458_0b254ea768_b.jpg Date=1457729661000 Size=12187213824
                                                                                     android.database.sqlite.SQLiteException: near "path": syntax error (code 1): , while compiling: INSERT INTO images(File path,name,Date,Size) VALUES (?,?,?,?)
                                                                                         at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                         at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:891)
                                                                                         at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:502)
                                                                                         at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                         at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                         at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                         at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                                         at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                                         at com.example.lukasz.myapplication2345.ImageCache.download(ImageCache.java:70)
                                                                                         at com.example.lukasz.myapplication2345.ImageCacher$1.doInBackground(ImageCacher.java:37)
                                                                                         at com.example.lukasz.myapplication2345.ImageCacher$1.doInBackground(ImageCacher.java:35)
                                                                                         at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                         at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:1)

我相信变量FILEPATH的列名称中的空格在您稍后尝试插入INTO时会导致语法错误。

将此列名称重新分配给更易于管理的名称。我建议更换&#34;文件路径&#34;以下划线,作为最佳实践。

请参阅此帖子Read data from sqlite where column name contains spaces in android