Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android sqlite数据库列错误_Android_Sqlite - Fatal编程技术网

Android sqlite数据库列错误

Android sqlite数据库列错误,android,sqlite,Android,Sqlite,我在应用程序中插入sqlite数据库时遇到问题。每当我尝试插入数据时,都会出现以下错误:android.database.sqlite.SQLiteException:table tblstudents没有名为town的列。我已尝试查看代码,但无法理解导致此错误的原因。我的代码如下: package com.StudentTracker; import java.sql.Blob; import android.content.ContentValues; import android.con

我在应用程序中插入sqlite数据库时遇到问题。每当我尝试插入数据时,都会出现以下错误:android.database.sqlite.SQLiteException:table tblstudents没有名为town的列。我已尝试查看代码,但无法理解导致此错误的原因。我的代码如下:

package com.StudentTracker;
import java.sql.Blob;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DBAdapter {

    public static final String KEY_ROWID = "id";
    public static final String KEY_STUDENTNAME = "studentname";
    public static final String KEY_DOB = "dob";
    public static final String KEY_ADDRESS1 = "address1";
    public static final String KEY_ADDRESS2 = "address2";
    public static final String KEY_TOWN = "town";
    public static final String KEY_POSTCODE = "postcode";
    public static final String KEY_PHONE = "phone";  
    public static final String KEY_STUDENT_PIC = "studentpic";  

    private static final String TAG = "DBAdapter"; 



    private static final String DATABASE_NAME = "studentDB2";
    private static final String DATABASE_TABLE = "tblstudents";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table tblstudents (id integer primary key autoincrement, "
        + "studentname text not null, dob text not null, " 
        + "address1 text not null, address2 text not null, "
        + "town text not null, postcode text not null, "
        + " phone text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS studentDB2");
            onCreate(db);
        }
    }    

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertStudent(String studentname, String dob, String address1, String address2, String town, String postcode, String phone) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_STUDENTNAME, studentname);
        initialValues.put(KEY_DOB, dob);
        initialValues.put(KEY_ADDRESS1, address1);
        initialValues.put(KEY_ADDRESS2, address2);
        initialValues.put(KEY_TOWN, town);
        initialValues.put(KEY_POSTCODE, postcode);
        initialValues.put(KEY_PHONE, phone);
        //initialValues.put(KEY_STUDENT_PIC, studentpic);

        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllStudents() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_STUDENTNAME,
                KEY_DOB,
                KEY_ADDRESS1,
                KEY_ADDRESS2,
                KEY_TOWN,
                KEY_POSTCODE,
                KEY_PHONE
                            }, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular title---
    public Cursor getStudent(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID, 
                        KEY_STUDENTNAME,
                        KEY_DOB,
                        KEY_ADDRESS1,
                        KEY_ADDRESS2,
                        KEY_TOWN,
                        KEY_POSTCODE,
                        KEY_PHONE                  
                        }, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateStudent(long rowId, String studentname, 
    String dob, String address1, String address2, String town,
    String postcode, String phone) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_STUDENTNAME, studentname);
        args.put(KEY_DOB, dob);
        args.put(KEY_ADDRESS1, address1);
        args.put(KEY_ADDRESS2, address2);
        args.put(KEY_TOWN, town);
        args.put(KEY_POSTCODE, postcode);
        args.put(KEY_PHONE, phone);
        //args.put(KEY_STUDENT_PIC, studentpic);

        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }




}

如果您最近添加了该列,请记住还要增加数据库版本,以便执行升级例程。

您应该在
OnUpgrade()
中删除表
tblstudents
,而不是
studentDB2

db.execSQL("DROP TABLE IF EXISTS tblstudents");
不要忘记增加
数据库\u版本的数量
。否则,
OnUpgrade()
将不会被调用

private static final int DATABASE_VERSION = 2;
//检查它^^数据库版本是否为一个新值(递增)

public TracksDB(上下文){
super(上下文,数据库名称,空,数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
创建最终字符串数据库=
创建表tblstudents(id整数主键自动递增)
+studentname文本不为空,dob文本不为空
+address1文本不为空,address2文本不为空
+城镇文本不为空,邮政编码文本不为空
+“电话文本不为空);”;
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
如果(旧版本<2){
最终字符串更改\u TBL=
“ALTER TABLE”+表格名称+
“添加列镇文本不为空;”;
db.execSQL(ALTER_TBL);
}
}
}

增加版本号后,它将通知数据库生成器注意onCreate方法中的CREATETABLES语句。一旦它看到新的内容或任何类型的差异,它将转到onUpgrade方法并运行代码,使其能够按照您的意愿运行,现在包括表列。

是否尝试删除SQLite数据库并再次创建?是否尝试清理和重建项目?此行可能会失败:db.execSQL(“如果存在studentDB2,则删除表”);我猜“studentDB2”是数据库,而不是表
public class DBaseHelper extends SQLiteOpenHelper {
private final static int    DB_VERSION = 2;
public TracksDB(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    final String DATABASE_CREATE =
        "create table tblstudents (id integer primary key autoincrement, "
        + "studentname text not null, dob text not null, " 
        + "address1 text not null, address2 text not null, "
        + "town text not null, postcode text not null, "
        + " phone text not null);";
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) {
        final String ALTER_TBL = 
            "ALTER TABLE " + TABLE_NAME +
            " ADD COLUMN town text not null;";
        db.execSQL(ALTER_TBL);
    }
}
}