SQLlite - 没有这样的列 [英] SQLlite - no such column

查看:160
本文介绍了SQLlite - 没有这样的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲$ P $对填充数据库并简单地检索数据。这是一个时间表它(目​​前)获得数据,并把它变成3列。活动名称,日期和放大器;位置。

我可以成功地检索ID号也就是PK和活动名称,但是当它面临的列日的应用程序关闭。

这是我收到的logcat错误:

 产生的原因:android.database.sqlite.SQLiteException:没有这样的列:天(code 1),在编译:选择_id,活动当天,位置,距离TrainingTable

我将我的包含code,看看是否有一个明显的错误,我很想念:

 公共类Training_table {//设置行
公共静态最后弦乐KEY_ROWID =_id; //创建一个行ID变量
公共静态最后弦乐KEY_ACTIVITY =活动;
公共静态最后弦乐KEY_DAY =天;
公共静态最后弦乐KEY_LOCATION =地利;//设置数据库为私有所以只有这个类可以交流
私有静态最后弦乐DATABASE_TABLE =TrainingTable;
私有静态最终诠释DATABASE_VERSION = 2; // DB版本//设置数据库和设置子类,并使用dbHelper设置分贝私人DbHelper ourHelper;
私人最终上下文ourContext;
私人SQLiteDatabase ourDatabase;私有静态类DbHelper扩展SQLiteOpenHelper {
    公共DbHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION); //传递信息到超类
    }    // EXE SQL code创建一个表&安培;三列
    //只有这个onCreate方法被调用时是我们第一次创建数据库    / * @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        // TODO自动生成方法存根
        db.execSQL(CREATE TABLE+ DATABASE_TABLE +(+
                KEY_ROWID +INTEGER PRIMARY KEY AUTOINCREMENT,+
                KEY_ACTIVITY +TEXT NOT NULL,+ KEY_DAY +TEXT NOT NULL,+
                KEY_LOCATION +TEXT NOT NULL);
        ); //执行数据库和创建表
    } // onCreate方法
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        db.execSQL(DROP TABLE IF EXISTS+ DATABASE_TABLE);
        的onCreate(DB);    }
}公共Training_table(上下文C){
    ourContext = C;
} //构造函数(初始化上下文)// openMethod&安培;写它
公共Training_table的open(){    ourHelper =新DbHelper(ourContext);    ourDatabase = ourHelper.getWritableDatabase();
    返回此;
}
//关闭SQLiteOpenHelper
公共无效的close(){
    ourHelper.close();
}
公共无效deleteTable(){
    ourDatabase.delete(DATABASE_TABLE,NULL,NULL);
}   众长新条目(字符串theActivity,字符串theDay,字符串theLocation){
   // TODO自动生成方法存根
   ContentValues​​值=新ContentValues​​();
   values​​.put(KEY_ACTIVITY,theActivity);
   values​​.put(KEY_DAY,theDay);
   values​​.put(KEY_LOCATION,theLocation);
   返回ourDatabase.insert(DATABASE_TABLE,空,价值);
   }
   公共字符串的getData(){// CREATING getData()方法
   的String [] =列新的String [] {KEY_ROWID,KEY_ACTIVITY,KEY_DAY,KEY_LOCATION}; / *设置列阵列,
                                                    拿着所有字段* /
   光标C = ourDatabase.query(DATABASE_TABLE,列,NULL,NULL,NULL,NULL,NULL); //初始化光标
   字符串结果=;    / *建立几个INT的下面,以保持活性的列的位置,
    日和位置数据库内* /
   INT indexActivity = c.getColumnIndex(KEY_ACTIVITY);
   INT indexDay = c.getColumnIndex(KEY_DAY);
   INT indexLocation = c.getColumnIndex(KEY_LOCATION);   为(c.moveToLast();!c.isAfterLast(); c.moveToNext()){       //获取以下的所有值
       结果= c.getString(indexActivity);
       结果=结果++ c.getString(indexDay)++ c.getString(indexLocation);
   }
   c.close(); //关闭游标
   返回结果;
   }
} // RaceRating类(DB类)

这是Java类调用方法:

 公共类Training_table_view延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.training_table_view);    TextView的电视=(的TextView)findViewById(R.id.tvTTinfo); //设置参考
    //按钮BV =(按钮)findViewById(R.id.bViewDetails);    字符串的行为=游泳;
    字符串日=星期一;
    串LOC =BFAST;    Training_table信息=新Training_table(本); //创建培训表    info.open(); //公开训练表类
    info.deleteTable();    //info.insertRecord1();
    info.newEntry(法,日,LOC);    字符串数据= info.getData(); //从获取数据的方法返回的字符串    info.close();    tv.setText(数据); //设置文本视图我们从得到的数据串
} //的onCreate
} // Training_table_view类


解决方案

 产生的原因:android.database.sqlite.SQLiteException:没有这样的列:天(code 1): ,在编译时:SELECT _id,活动当天,位置,距离TrainingTable

您可能已经添加的列在后期(即正在运行的应用previously)

您需要清除应用程序的数据,重新安装并再次检查。

打开设备设置>应用程序管理器/管理应用> YourAppName。

在这里,你会发现明确的数据按钮,并卸载。

希望这有助于。

I want to pre-populate a database and simply retrieve the data. It is a timetable which (for the moment) takes data and puts it into 3 columns. activity name, day & location.

I can successfully retrieve the ID number which is the PK and activity name but the application shuts down when it is faced with the column day.

This is the logcat error I am receiving:

Caused by: android.database.sqlite.SQLiteException: no such column: day (code 1): , while compiling: SELECT _id, activity, day, location FROM TrainingTable

I will include my code to see if there is a glaring mistake I'm missing:

public class Training_table {

//setting up rows
public static final String KEY_ROWID = "_id";//creates a row ID variable
public static final String KEY_ACTIVITY = "activity";
public static final String KEY_DAY = "day";
public static final String KEY_LOCATION = "location";

//setting up database as private so only this class can ac
private static final String DATABASE_TABLE = "TrainingTable";
private static final int DATABASE_VERSION = 2;//db version

//setup db and setup subclass and use dbHelper to setup db

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;



private static class DbHelper extends SQLiteOpenHelper{


    public DbHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);//passing info into super class
    }

    //exe SQL code to create a table & three columns
    //only time this onCreate method is called is first time we create db

    /*@Override
    public void onCreate(SQLiteDatabase db) {


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_ACTIVITY + " TEXT NOT NULL, " + KEY_DAY + " TEXT NOT NULL, " +
                KEY_LOCATION + " TEXT NOT NULL);"
        );//Execute Database and create table


    }//onCreate method


    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " +DATABASE_TABLE);
        onCreate(db);

    }
}

public  Training_table(Context c){
    ourContext = c;
}//constructor (initialising context)



//openMethod & write to it
public Training_table open(){

    ourHelper = new DbHelper(ourContext);

    ourDatabase = ourHelper.getWritableDatabase();
    return  this;
}
//close SQLiteOpenHelper
public void close(){
    ourHelper.close();
}
public void deleteTable(){
    ourDatabase.delete(DATABASE_TABLE, null, null);
}

   public long newEntry(String theActivity, String theDay, String theLocation) {
   // TODO Auto-generated method stub
   ContentValues values = new ContentValues();
   values.put(KEY_ACTIVITY, theActivity);
   values.put(KEY_DAY, theDay);
   values.put(KEY_LOCATION, theLocation);
   return ourDatabase.insert(DATABASE_TABLE, null, values);
   }


   public String getData() { //CREATING getData() method
   String[] columns = new String[] {KEY_ROWID, KEY_ACTIVITY, KEY_DAY, KEY_LOCATION}; /*Setting up column array,
                                                    holding all fields*/
   Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); //initialising the cursor
   String result = "";

    /*Setting up a few int's below, to hold the column location of the activity,
    day and location within the database */
   int indexActivity = c.getColumnIndex(KEY_ACTIVITY);
   int indexDay = c.getColumnIndex(KEY_DAY);
   int indexLocation = c.getColumnIndex(KEY_LOCATION);

   for (c.moveToLast(); !c.isAfterLast(); c.moveToNext()){

       //Getting all values below
       result = c.getString(indexActivity);
       result = result + " " + c.getString(indexDay) + " " + c.getString(indexLocation);
   }
   c.close(); //Closing the cursor
   return result;
   }


}//RaceRating class (DB Class)

And this is the java class calling the methods:

public class Training_table_view extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.training_table_view);

    TextView tv = (TextView)findViewById(R.id.tvTTinfo);//setting up reference
    //Button bv = (Button)findViewById(R.id.bViewDetails);

    String act = "Swimming";
    String day = "Mon";
    String loc = "bfast";

    Training_table info = new Training_table(this);//create training table

    info.open();//open Training table  class
    info.deleteTable();

    //info.insertRecord1();
    info.newEntry(act,day,loc);

    String data = info.getData();//return string from get data method

    info.close();

    tv.setText(data);//set text view to the string we got data from


}//onCreate


}//Training_table_view class

解决方案

Caused by: android.database.sqlite.SQLiteException: no such column: day (code 1): , while compiling: SELECT _id, activity, day, location FROM TrainingTable

You might have added column day on later stage (i.e. running application previously)

You need to Clear the data of your application, Reinstall and check again.

Open device Settings > Application Manager / Manage Apps > YourAppName.

Here you will find a button of clear data, and uninstall.

Hope this helps.

这篇关于SQLlite - 没有这样的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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