【问题标题】:Inserting Row into Sqlite DB(Android)将行插入 Sqlite DB(Android)
【发布时间】:2013-02-07 00:21:32
【问题描述】:

我有表“日志”:

private static final String DATABASE_CREATE = 
            "create table if not exists logs (_id integer primary key autoincrement, "+ "date datetime, city text, " + "type text, log text, nick text);";

以及插入日志的方法:

public long insertLog(String city, int type, String log, String nick) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();

        ContentValues initialValues = new ContentValues();

        initialValues.put(KEY_DATE, dateFormat.format(date));
        initialValues.put(KEY_CITY, city);
        initialValues.put(KEY_TYPE, type);
        initialValues.put(KEY_LOG, log);
        initialValues.put(KEY_NICK, nick);

        return db.insert(DATABASE_TABLE, null, initialValues);

    }

每次我尝试插入日志时,都没有异常。方法每次返回“1”。但是当我用 sqliteviewer 查看我的桌子时,我发现它是空的。这个问题的原因可能是什么?

【问题讨论】:

  • 您如何使用 sqliteviewer 访问应用程序的数据库?
  • 我正在从 android app 文件夹中提取数据库。我正在使用安卓模拟器。
  • 好的,应该可以。您是在运行应用程序后执行此操作的吗?
  • 我发现了问题,我将注释传递给 DbAdapter 构造函数,我在其中设置数据库名称。我不知道我怎么没有得到任何例外。无论如何谢谢:)
  • 这很奇怪,也许它创建了第二个数据库......不过很高兴你找到了答案!

标签: android database sqlite sql-insert


【解决方案1】:

这样创建表

private static final String DATABASE_CREATE = 
            "create table if not exists logs (_id integer primary key autoincrement,date text, city text, type text, log text, nick text);";

& 像这样插入

public long insertLog(String city, int type, String log, String nick) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();

        ContentValues initialValues = new ContentValues();

         // convert date to string
        String dateString = dateFormat.format(date);

        initialValues.put(KEY_DATE,dateString);
        initialValues.put(KEY_CITY, city);
        initialValues.put(KEY_TYPE, type);
        initialValues.put(KEY_LOG, log);
        initialValues.put(KEY_NICK, nick);

        return db.insert(DATABASE_TABLE, null, initialValues);

    }

编辑:

调用这个方法

         db.open();
         db.insertLog(....);
         db.close();

【讨论】:

  • 这实际上改变了什么?您只是删除了几个引号并创建了一个额外的变量,数据看起来一样......
  • 不知何故我发现了问题,谢谢你的尝试。问题不在这里。
  • 这是我的数据库名称,我传递给构造函数 NULL。奇怪的是我没有得到任何例外。
【解决方案2】:

完整示例 here

DataBaseHelper 中定义基本配置后,使用 db 对象使用以下方法插入行

// method to insert a record in Table
public static String insertEntry(String user_name, String user_phone, String user_email)
{

    try {


        ContentValues newValues = new ContentValues();
        // Assign values for each column.
        newValues.put("user_name", user_name);
        newValues.put("user_phone", user_phone);
        newValues.put("user_email", user_email);

        // Insert the row into your table
        db = dbHelper.getWritableDatabase();
        long result=db.insert(TABLE_NAME, null, newValues);
        toast("User Info Saved! Total Row Count is "+getRowCount());
        db.close();

    }catch(Exception ex) {
    }
    return "ok";
}

【讨论】:

    猜你喜欢
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多