尝试在SQLite数据库中插入记录时出现以下错误 -
android.database.sqlite.SQLiteException:table jolly没有名为noOfShares的列(代码1):,编译时:INSERT INTO jolly(noOfShares,id,noOfMustGos,noOfSeen,caption,noOfBeenTheres,location)VALUES(?,? ,?,?,?,?,?)
我无法理解为什么按此顺序显示列:
当我创建具有不同列顺序的表并以相同顺序分配值时。我尝试卸载应用程序并更改表名但它没有解决。
适配器:
VideoSql videoSql;
public View getView(final int i, View v, ViewGroup viewGroup) {
videoSql=new VideoSql();
DatabaseHandler db = new DatabaseHandler(mContext);
videoSql.setId(mVideos.get(i).getId());
videoSql.setCaption(mVideos.get(i).getCaption());
videoSql.setNoOfMustGos(mVideos.get(i).getNoOfMustGos());
videoSql.setNoOfShares(mVideos.get(i).getNoOfShares());
videoSql.setLocation(mVideos.get(i).getLocation());
videoSql.setNoOfBeenTheres(mVideos.get(i).getNoOfBeenTheres());
videoSql.setNoOfSeen(mVideos.get(i).getNoOfSeen());
db.addContact(videoSql);
}
数据库处理器:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
VideoSql mvideo;
// Database Name
private static final String DATABASE_NAME = "TDatabase";
// Contacts table name
private static final String TABLE_JOLLY = "jolly";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_CAPTION = "caption";
private static final String KEY_LOCATION = "location";
private static final String KEY_NOSHARES = "noOfShares";
private static final String KEY_NOBEENTHERE = "noOfBeenTheres";
private static final String KEY_NOMUSTGO = "noOfMustGos";
private static final String KEY_NOCOMMENTS = "noOfComments";
private static final String KEY_NOSEEN = "noOfSeen";
private static final String KEY_USERNAME = "userName";
private static final String KEY_USERID = "userId";
private static final String KEY_TIMESTAMP ="timestamp" ;
private static final String KEY_FOLLOWED = "followed";
private static final String KEY_BEENTHERE = "beenThere";
private static final String KEY_MUSTGO = "mustGo";
private static final String KEY_NOFOLLOWED = "noOfFollowed";
private static final String KEY_NOFOLLOWERS = "noOfFollowers";
private static final String KEY_USERHANDLE = "userHandle";
private static final String KEY_DTS = "dts";
private static final String KEY_FEATURED = "featured";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_JOLLY_TABLE = "CREATE TABLE " + TABLE_JOLLY + "("
+ KEY_ID + KEY_CAPTION + KEY_LOCATION + KEY_NOSHARES + KEY_NOBEENTHERE +KEY_NOMUSTGO +
KEY_NOSEEN +")" ;
db.execSQL(CREATE_JOLLY_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_JOLLY);
onCreate(db);
}
public void addContact(VideoSql videoSql)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, videoSql.getId());
values.put(KEY_CAPTION, videoSql.getCaption());
values.put(KEY_LOCATION, videoSql.getLocation());
values.put(KEY_NOSHARES, videoSql.getNoOfShares());
values.put(KEY_NOBEENTHERE, videoSql.getNoOfBeenTheres());
values.put(KEY_NOMUSTGO, videoSql.getNoOfMustGos());
values.put(KEY_NOSEEN, videoSql.getNoOfSeen());
db.insert(TABLE_JOLLY, null, values);
Log.e("addcontact", videoSql.getCaption());
db.close(); // Closing database connection
}
// Getting All Contacts
public List<VideoSql> getAllContacts() {
List<VideoSql> contactList = new ArrayList<VideoSql>();
String selectQuery = "SELECT * FROM " + TABLE_JOLLY;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst())
{
do
{
VideoSql videoinfo = new VideoSql();
videoinfo.setId(cursor.getString(0));
videoinfo.setCaption(cursor.getString(1));
videoinfo.setLocation(cursor.getString(2));
videoinfo.setNoOfShares(cursor.getString(3));
videoinfo.setNoOfBeenTheres(cursor.getString(4));
videoinfo.setNoOfMustGos(cursor.getString(5));
videoinfo.setNoOfSeen(cursor.getString(6));
contactList.add(videoinfo);
}
while (cursor.moveToNext());
}
return contactList;
}
}
答案 0 :(得分:2)
在SQL CREATE TABLE
命令中的列名后面提供列类型,如
String DATABASE_CREATE = "create table "
+ TABLE_JOLLY + "(" + KEY_ID + " integer primary key autoincrement, "
+ KEY_CAPTION +" text not null, "
+ KEY_LOCATION +" text not null, "
+ KEY_NOSHARES +" text not null, "
+ KEY_NOBEENTHERE +" text not null, "
+ KEY_NOMUSTGO+" text not null, "
+ KEY_NOSEEN " text not null);"
有关信息http://www.vogella.com/tutorials/AndroidSQLite/article.html
答案 1 :(得分:1)
实际上,您在create table语法中缺少每列的数据类型。
您应该使用以下语法创建表,
String CREATE_JOLLY_TABLE = "CREATE TABLE " + TABLE_JOLLY + "("
+ KEY_ID + " integer primary key autoincrement,"
......<other column-name> <column-type>; ///
注意:上面建议进行更改后,您应首先从设备中删除旧应用程序,然后再次测试,否则它将继续给出相同的错误,因为它不会执行onCreate(SQLiteDatabase db)
方法,因为它已经使用旧实例执行。因此,要执行onCreate(SQLiteDatabase db)
的代码,建议从设备中删除完全旧的应用程序并执行新的代码。