Android SQLite插入语句错误 [英] Android SQLite insert statement error

查看:130
本文介绍了Android SQLite插入语句错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中,我修改了表.以前它有2列(不包括ID).现在,它具有3列(不包括ID).因此,每当我添加一个元组时,它就会显示错误. 为了添加元组,我称为addContact函数

In my database I modified my table. previously it had 2 columns excluding id. Now it has 3 columns excluding id. So whenever i add an tuple it shows error. For adding tuple i ve called addContact function

      private static final int DATABASE_VERSION = 25;


    // Database Name
    private static final String DATABASE_NAME = "contactsManager";

    // Contacts table name
    private static final String TABLE_CONTACTS1 = "contacts";

    // Contacts Table Columns names

    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";
    private static final String KEY_UNIQUE = "unique";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS1 + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_UNIQUE + " INTEGER," + KEY_PH_NO + " TEXT " + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS1);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new contact
    void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_UNIQUE, contact.getUnique());
        values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone

        // Inserting Row
         //giving error at following line
        db.insert(TABLE_CONTACTS1, null, values);
        db.close(); // Closing database connection
    }

//错误是插入名称= 21:58唯一= 1717 phone_number = null3时出错 android.database.sqlite.SQLiteException:在"unique"附近:语法错误(代码1):,在编译时:INSERT INTO contacts(姓名,唯一性,phone_number)VALUES(?,?,?)

//error is Error inserting name=21:58 unique=1717 phone_number=null3 android.database.sqlite.SQLiteException: near "unique": syntax error (code 1): , while compiling: INSERT INTO contacts(name,unique,phone_number) VALUES (?,?,?)

推荐答案

UNIQUE是SQL中的关键字,因此不能用作标识符.

UNIQUE is a keyword in SQL and cannot be used as an identifier as such.

将列名重命名为isunique或类似名称,然后卸载您的应用程序,以使修改后的onCreate()实际上运行.

Rename the column name to something like isunique or similar, and uninstall your app so that the modified onCreate() is actually run.

这篇关于Android SQLite插入语句错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆