Android - SQlite插入不插入

时间:2015-05-01 11:52:48

标签: java android sqlite

所以我试图在内部sqlite数据库中插入一些数据但是在我运行插入后没有添加任何数据。据我所知,日志中没有错误,我显示的每个调试日志都显示出来。如果我尝试运行在sqlitestudio中的日志中返回的查询,它的工作没有问题,所以我没有得到关于出错的线索。

@Override
public void onCreate(SQLiteDatabase db) {
    String SQL = pictureTable();
    db.execSQL(SQL);
}

private String pictureTable() {
    return "CREATE TABLE geophoto_db_pictures ( picid integer,"
            + "name varying character(50),"
            + "city varying character(20) NOT NULL,"
            + "zipcode varying character(20) NOT NULL,"
            + "country varying character(20) NOT NULL,"
            + "picdate datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,"
            + "tags varying character(200),"
            + "image varying character(200) NOT NULL,"
            + "uploaded integer NOT NULL DEFAULT 0, PRIMARY KEY (picid))";
}

@Override
public void savePicture(Picture pic) {
    Log.d(LOG_TAG, "saving picture started. Data: " + pic.getName());

    // clean the inputs
    String name = pic.getName();
    String city = pic.getCity();
    if (city != null) {
        city = "'" + city + "'";
    }
    String country = pic.getCountry();
    if (country != null) {
        country = "'" + country + "'";
    }
    String zip = pic.getZipcode();
    if (zip != null) {
        zip = "'" + zip + "'";
    }
    String tags = tagsToString(pic.getTags());
    String image = pic.getImage();

    // Insert Query, all possible null values on "not null" rows will be
    // replaced by a default value.
    String SQL = "INSERT INTO geophoto_db_pictures(name, city, zipcode, country, tags, image)"
            + "VALUES('"
            + name
            + "',"
            + "IFNULL("
            + city
            + ", 'Unknown')"
            + ","
            + "IFNULL("
            + zip
            + ", 'Unknown')"
            + ","
            + "IFNULL("
            + country + ",'Unknown')" + ",'" + tags + "','" + image + "')";
    Log.d(LOG_TAG, SQL);
    executeWriteQuery(SQL);
    ArrayList<Picture> list = getAllPictures();
    Log.d(LOG_TAG, "Size :"+list.size());
}

private Cursor executeWriteQuery(String query){
    Log.d(LOG_TAG, "execute write query");
    SQLiteDatabase db = getWritableDatabase();
    Cursor response = db.rawQuery(query, null);
    Log.d(LOG_TAG, "write query executed");
    return response;
}

所有提示/帮助非常感谢!

托马斯

2 个答案:

答案 0 :(得分:1)

您遇到的问题是,当您应该使用rawQuery()而不是see this answer时,您尝试使用execSQL()插入记录。

因此,executeWriteQuery的正确代码如下:

private void executeWrite(String command){
    Log.d(LOG_TAG, "execute write");
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL(command); 
    Log.d(LOG_TAG, "write executed");
}

此外,请考虑使用insert(),因为这样可以获取返回值以确定数据是否已成功插入。

答案 1 :(得分:1)

尝试在表创建查询结束时放置分号。在您的情况下如下所示

build.js

在通过外部private String pictureTable() { return "CREATE TABLE geophoto_db_pictures ( picid integer," + "name varying character(50)," + "city varying character(20) NOT NULL," + "zipcode varying character(20) NOT NULL," + "country varying character(20) NOT NULL," + "picdate datetime NOT NULL DEFAULT CURRENT_TIMESTAMP," + "tags varying character(200)," + "image varying character(200) NOT NULL," + "uploaded integer NOT NULL DEFAULT 0, PRIMARY KEY (picid));"; } 提供查询时,您需要提供String查询语句结尾SQL。使用原语;不需要SQLite,因为它只需要参数并创建函数查询本身。我遇到过这两种情况,最后我才明白我把它放在这里的方式。