当前位置: 动力学知识库 > 问答 > 编程问答 >

android.database.sqlite.SQLiteException:table abc has no column named xyz

问题描述:

I am getting following error while trying to insert records in SQLite database-

android.database.sqlite.SQLiteException: table jolly has no column named noOfShares (code 1): , while compiling: INSERT INTO jolly(noOfShares,id,noOfMustGos,noOfSeen,caption,noOfBeenTheres,location) VALUES (?,?,?,?,?,?,?)

I failed to understand why its showing column in this order:

  • noOfShares
  • id
  • noOfMustGos
  • noOfSeen
  • caption
  • noOfBeenTheres
  • location

while I created table with different order of columns and assigning value in same order. I tried uninstalling the app and also changing table name but it didn't resolved.

Adapter:

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);

}

DatabaseHandler:

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;

}

}

网友答案:

You are actually missing the each column's data type in the create table syntax.

You should create table with following syntax,

String CREATE_JOLLY_TABLE = "CREATE TABLE " + TABLE_JOLLY + "("
            + KEY_ID + " integer primary key autoincrement,"
......<other column-name> <column-type>; ///

Note : Once you made changes suggested above, you should first remove the old application from your device and then test it again, otherwise it will keep on giving you same error as it will not execute onCreate(SQLiteDatabase db) method since it has been executed with old instance. So to execute onCreate(SQLiteDatabase db)'s code it is advisable to remove completely old application from the device and execute the code as fresh.

网友答案:

Provide Column Type after Column Name in SQL CREATE TABLE Command like

Example

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);"

For info http://www.vogella.com/tutorials/AndroidSQLite/article.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: