You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio报错:已创建列却提示列不存在?

Fix: SQLite Error "table contacts has no column named name" when Inserting

Hey, let's break down what's going on here. Your error log says the contacts table doesn't have a name column, but looking at your code, you did define that column in your CREATE statement. The problem is a typo in your onCreate method—and it's a classic easy-to-miss mistake!

Look at this snippet from your onCreate:

public void onCreate(SQLiteDatabase db) {
    String create = "CREATE TABLE " + TABLE_CONTACTS + "("+ KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT"
            + ")";
    db.execSQL(create2); // <-- Here's the critical mistake!
}

You built your correct table creation SQL and stored it in the create variable, but you tried to execute create2 instead. That variable doesn't match the valid SQL you wrote, so the actual table created in your database doesn't have the name or phone_number columns you expected.

Step-by-Step Fixes:

  1. Fix the typo immediately: Change db.execSQL(create2); to db.execSQL(create); so your proper table structure gets executed.
  2. Clear the old broken table: Since your app already created a table with the wrong structure, you need to reset it:
    • Uninstall your app from the emulator/device, then reinstall it to start fresh.
    • Or, increment your database version number (the integer passed to your SQLiteOpenHelper constructor) to trigger the onUpgrade method, which will drop the old table and rebuild it with the correct structure.

Here's the corrected onCreate method for reference:

public void onCreate(SQLiteDatabase db) {
    String create = "CREATE TABLE " + TABLE_CONTACTS + "("+ KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT"
            + ")";
    db.execSQL(create); // Now using the right variable!
}

Once you make these changes, your addContact method should work as expected—since the table will finally have the columns you're trying to insert into.

内容的提问来源于stack exchange,提问作者Abo Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 14:43:12