在浏览器中使用的sqlite中的INSERT查询,但不在android中

时间:2012-04-04 18:01:33

标签: android eclipse sqlite

我真的花了很多时间在这段代码上,但我无法解释为什么数据库无法正常工作

我想只插入一行。所以我创建了这个函数:

void insertfav(String k){

    myDataBase = khol();
    Cursor cur = myDataBase.rawQuery("SELECT " + colname + " from "
            + deptTable + " WHERE " + col3 + "=" + "'" + k + "'",
            new String[] {});
    cur.moveToFirst();
    String area = cur.getString(cur.getColumnIndexOrThrow(colname));
    cur = myDataBase.rawQuery("SELECT " + col2 + " from " + deptTable
            + " WHERE " + col3 + "=" + "'" + k + "'", new String[] {});
    cur.moveToFirst();
    String disease = cur.getString(cur.getColumnIndexOrThrow(col2));
    myDataBase.rawQuery(
            "INSERT INTO " + favtable + " ("+colname+", "+col2+", "+col3+")"
                    + "Values (" + "'" + area + "'" + ", " + "'" + disease
                    + "'" + ", " + "'" + k + "'" + ")", new String[] {});
    myDataBase.close();
}

当我在sqlite浏览器中运行它时,此查询工作正常,但是当在模拟器中运行应用程序时实际调用此函数时,不会插入任何内容。

khol()是一个打开数据库的函数。这是khol()函数的代码:

public SQLiteDatabase khol() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
    return myDataBase;
}

其中DB_PATH和DB_NAME是:

private static String DB_PATH = "/data/data/example.SQLitetest/";

private static String DB_NAME = "desicures.db";

虽然khol()在其他查询中正常工作,但可能是我在这里做错了...

Plz帮助我......

4 个答案:

答案 0 :(得分:1)

您可能想尝试使用myDatabase.insert()而不是rawQuery。

答案 1 :(得分:1)

docrawQuery

Runs the provided SQL and returns a Cursor over the result set.

execSQL(String sql)

Execute a single SQL statement that is NOT a SELECT or any other SQL statement 
that returns data.

    Execute a single SQL statement that is not a query. For example, CREATE TABLE, 
DELETE,INSERT, etc. Multiple statements separated by ;s are not supported. it takes a 
write lock

尝试使用execSQL而不是rawQuery.like

myDataBase.execSQL(
            "INSERT INTO " + favtable + " ("+colname+", "+col2+", "+col3+")"
                    + "Values (" + "'" + area + "'" + ", " + "'" + disease
                    + "'" + ", " + "'" + k + "'" + ");");

答案 2 :(得分:0)

方法rawQuery()返回游标,你不能使用这个方法进行插入,所以

myDataBase.rawQuery(
        "INSERT INTO " + favtable + " ("+colname+", "+col2+", "+col3+")"
                + "Values (" + "'" + area + "'" + ", " + "'" + disease
                + "'" + ", " + "'" + k + "'" + ")", new String[] {});

是错误的方法。

你可以使用insert()方法,你可以尝试跟随

ContentValues values = new ContentValues();
values.put(colname , area);
values.put(col2 , disease);
values.put(col3 , k);
long rowID = db.insert(favtable, null, values);
System.out.println("Added Row No: "+rowID);

答案 3 :(得分:0)

关于代码的三件事

  • execSQL而不是rawQuery
  • ?中使用String[]和params - 更安全,可以处理k = "ha'ck"
  • 很好,关闭游标

喜欢这样:

myDataBase = khol();
Cursor cur = myDataBase.rawQuery("SELECT " + colname + " from "
        + deptTable + " WHERE " + col3 + "=?",
        new String[] { k });
cur.moveToFirst();
String area = cur.getString(cur.getColumnIndexOrThrow(colname));
cur.close();
cur = myDataBase.rawQuery("SELECT " + col2 + " from " + deptTable
        + " WHERE " + col3 + "=?", new String[] { k });
cur.moveToFirst();
String disease = cur.getString(cur.getColumnIndexOrThrow(col2));
cur.close();

// either
myDataBase.execSQL(
        "INSERT INTO " + favtable + " ("+colname+", "+col2+", "+col3+")"
                + "Values (?, ?, ?)",
                new String[] { area, disease, k });
// or 
ContentValues cv = new ContentValues();
cv.put(colname, area);
cv.put(col2, disease);
cv.put(col3, k);
long newRowId = myDataBase.insert(favtable, colname, cv);

myDataBase.close();

此外,您可能需要检查游标是否为null /包含值。 cur.getString可能会给您NullPointerException或错误,因为没有列(如果您的数据库没有数据)。

此外,您的查询不需要多个查询,您可以一次请求多个列:

cur = myDataBase.rawQuery("SELECT " + colname + "," + col2 + " from "
        + deptTable + " WHERE " + col3 + "=?", new String[] { k, k });
if (cur.moveToFirst()) {
    // starting at 0 columns are ordered in the way you select them
    String area = cur.getString(0);
    String disease = cur.getString(1);
}