繁体   English   中英

Android-SQlite插入未插入

[英]Android - SQlite insert not inserting

所以我试图在内部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;
}

所有提示/帮助深表感谢!

汤玛士

您面临的问题是,当您应该改用execSQL()时,尝试使用rawQuery()插入记录( 请参见此答案 )。

因此, 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()代替,因为这将使您获得返回值以确定是否成功插入了数据。

尝试在表创建查询的末尾添加分号。 您的情况如下所示

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 ; 因为它只需要参数并创建函数查询本身。 我经历了两种情况,最终了解了我在这里所说的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM