2

大家早上好;

我正在尝试在android中创建一个数据库类,它已经完成,我也成功创建了一个表。当我想使用 向表中插入行时,问题就来了ContentValues,它尝试以与表字段不同的顺序插入行,这会导致 logcat 中出现错误:没有这样的表,如下所示:

这些是我的变量:

    private static final String row_ID="RID";
    private static final String row_NAME="NAME";
    private static final String row_EMAIL="EMAIL";
    private static final String row_WEBSITE="WEBSITE";
    private static final String row_TELEPHONE1="PHONE_NUMBER1";
    private static final String row_TELEPHONE2="PHONE_NUMBER2";
    private static final String row_TELEPHONE3="PHONE_NUMBER3";
    private static final String row_TELEPHONE4="PHONE_NUMBER4";
    private static final String row_TELEPHONE5="PHONE_NUMBER5";
    private static final String db_NAME="ContactsDb";
    private static final String tab_NAME="ContactsTab";
    private static final int db_VERSION=2;
    private dbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

我的桌子:

    String CREATE_TABLE="CREATE TABLE " + db_NAME + " ("
                            + row_ID + " INT PRIMARY KEY AUTOINCREMENT,"
                            + row_NAME + " TEXT, "
                            + row_EMAIL + " TEXT, "
                            + row_WEBSITE + " TEXT, "
                            + row_TELEPHONE1 + " TEXT, "
                            + row_TELEPHONE2 + " TEXT, "
                            + row_TELEPHONE3 + " TEXT, "
                            + row_TELEPHONE4 + " TEXT, "
                            + row_TELEPHONE5 + " TEXT);";
        db_name.execSQL(CREATE_TABLE);

使用插入行ContentValues

    public long createEntry(String name, String email, String website, String telephone1, String telephone2, String telephone3, String telephone4, String telephone5)
{
    ContentValues cv = new ContentValues ();

    Log.v("STATUS", "getting input data..");

    cv.put(row_NAME, name);
    cv.put(row_EMAIL, email);
    cv.put(row_WEBSITE, website);       
    cv.put(row_TELEPHONE1, telephone1);
    cv.put(row_TELEPHONE2, telephone2);
    cv.put(row_TELEPHONE3, telephone3);
    cv.put(row_TELEPHONE4, telephone4);
    cv.put(row_TELEPHONE5, telephone5);
    Log.v("STATUS", "got input data, inserting data....");

    return ourDatabase.insert(tab_NAME, null, cv);
}

因此,当我运行此应用程序时,它会成功完成所有这些操作,直到它到达ourDatabase.insert(tab_NAME, null, cv);它显示错误:

    SQLiteLog            (1) no such table: ContactsTab
    SQLiteDatabase       Error inserting Name=Ahlam M. Hussain   PHONE_NUMBER5=
                         PHONE_NUMBER4=   PHONE_NUMBER3=    PHONE_NUMBER2=059999999
                         PHONE_NUMBER1= 09299999    EMAIL=ahlam@ahlam.com    EWEBSITE=
    SQLiteDatabase       android.database.sqlite.SQLiteException: no such table:
                         ContactsTab (code 1): ,while compiling: INSERT INTO
                         ContactsTab(NAME, PHONE_NUMBER5 PHONE_NUMBER4 PHONE_NUMBER3
                         PHONE_NUMBER2 PHONE_NUMBER1, EMAIL, WEBSITE) VALUES
                         (?,?,?,?,?,?,?,?)

您会注意到它正在尝试在不同的字段 orderm 中插入行,因此它无法识别表。

那么该怎么办 ???我真的很讨厌那个..

4

1 回答 1

1

你忘记戴上支架

  • row_TELEPHONE5 + "文本;";

它一定要是

  • row_TELEPHONE5 + " TEXT);";

编辑

用“CREATE TABLE IF NOT EXISTS”和“INTEGER PRIMARY KEY AUTOINCREMENT”而不是“INT PRIMARY KEY AUTOINCREMENT”试试。此外,您在 CREATE-TABLE 字符串中设置了 db_name 而不是 table_name。将其更改为您的 table_name:

     String CREATE_TABLE="CREATE TABLE IF NOT EXISTS " + tab_NAME + " ("
                        + row_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                        + row_NAME + " TEXT, "
                        + row_EMAIL + " TEXT, "
                        + row_WEBSITE + " TEXT, "
                        + row_TELEPHONE1 + " TEXT, "
                        + row_TELEPHONE2 + " TEXT, "
                        + row_TELEPHONE3 + " TEXT, "
                        + row_TELEPHONE4 + " TEXT, "
                        + row_TELEPHONE5 + " TEXT);";
    db_name.execSQL(CREATE_TABLE);
于 2012-12-21T10:56:12.483 回答