表*没有名为**的列

时间:2014-01-13 14:03:04

标签: java android sqlite android-sqlite

我正在尝试学习SQLite,尝试基本的东西和(正如预期的那样)它并没有真正起作用。 我试图创建另一个DB(不同的文件),但错误保持不变。请指导我。我是Android的新手,所以我没有太多的知识。

package net.multiplesystem.nosms.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class RegisterAdapter
{
    static final String DATABASE_NAME = "register.db";
    static final int DATABASE_VERSION = 1;
    public static final int NAME_COLUMN = 1;
    // TODO: Create public field for each column in your table.
    // SQL Statement to create a new database.
    static final String DATABASE_CREATE = "create table "+"REGISTER"+
            "( " +"ID"+" integer primary key autoincrement,"+ "NAME text,"+"NUMBER text); ";
    // Variable to hold the database instanceSTER
    public  SQLiteDatabase db;
    // Context of the application using the database.
    private final Context context;
    // Database open/upgrade helper
    private DataBaseHelper dbHelper;
    public  RegisterAdapter(Context _context)
    {
        context = _context;
        dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public  RegisterAdapter open() throws SQLException
    {
        db = dbHelper.getWritableDatabase();
        return this;
    }
    public void close()
    {
        db.close();
    }

    public  SQLiteDatabase getDatabaseInstance()
    {
        return db;
    }


    public void insertEntry(String userName,String number)
    {
        ContentValues newValues = new ContentValues();
        // Assign values for each row.
        newValues.put("NUMBER",number);
        newValues.put("NAME", userName);


        // Insert the row into your table
        db.insert("REGISTER", null, newValues);
        ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
    }
}

这是我的logcat:

01-13 08:55:49.269: E/SQLiteLog(2588): (1) table REGISTER has no column named NAME
01-13 08:55:49.319: E/SQLiteDatabase(2588): Error inserting NAME=hhhh NUMBER=564
01-13 08:55:49.319: E/SQLiteDatabase(2588): android.database.sqlite.SQLiteException: table REGISTER has no column named NAME (code 1): , while compiling: INSERT INTO REGISTER(NAME,NUMBER) VALUES (?,?)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
01-13 08:55:49.319: E/SQLiteDatabase(2588):     at net.multiplesystem.nosms.database.RegisterAdapter.insertEntry(RegisterAdapter.java:65)

package net.multiplesystem.nosms.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper
{
    public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
    {
        super(context, name, factory, version);
    }
    // Called when no database exists in disk and the helper class needs
    // to create a new one.
    @Override
    public void onCreate(SQLiteDatabase _db)
    {
        _db.execSQL(RegisterAdapter.DATABASE_CREATE);

    }
    // Called when there is a database version mismatch meaning that the version
    // of the database on disk needs to be upgraded to the current version.
    @Override
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
    {
        // Log the version upgrade.
        Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");

        // Upgrade the existing database to conform to the new version. Multiple
        // previous versions can be handled by comparing _oldVersion and _newVersion
        // values.
        // The simplest case is to drop the old table and create a new one.
        _db.execSQL("DROP TABLE IF EXISTS " + "REGISTER");
        // Create a new one.
        onCreate(_db);
    }
}

2 个答案:

答案 0 :(得分:2)

你真的创建了这张桌子吗?似乎永远不会执行DATABASE_CREATE脚本。 如果您已创建一次然后更改了语句,请确保通过递增数据库版本再次执行该语句。

DATABASE_VERSION = 1,您是否有没有NAME列的先前版本?尝试递增DATABASE_VERSION(可能添加一些日志记录/断点)并查看是否重新创建了数据库。

答案 1 :(得分:0)

这可能是因为您在添加属性之前迁移了表文件。 我建议你删除你的数据库文件 在数据库文件夹中重新创建一个空的数据库文件,然后迁移。 并重新运行您的服务器,这应该可以工作。