如何解决没有这样的表,同时插入在源码数据库异常 [英] how to solve no such table while inserting exception in sqlite data base

查看:81
本文介绍了如何解决没有这样的表,同时插入在源码数据库异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SQLite和我写的数据库类,并试图在数据库表中插入值,但我得到的例外是:

  I /数据库(958):sqlite的返回:错误code = 1,味精=没有这样的表:运动

,当我固定的记录它指向此语句:

 长D = dbAdapter.SaveExecise(日期,exercise_Time,ex_Name,分,burned_cals);

为什么这引发异常?我已阅读并尝试过这样的问题解决方案,但错误依然我怎么能解决呢?
这是我的code:

 公共类DBAdapter {  私有静态最后弦乐DB_NAME =MYDB;
  私有静态最终诠释DB_VERSION = 1;
  私有静态最后弦乐EXERCISE_TABLE_NAME =锻炼;
  私有静态最后弦乐TAG =DBAdapter;
  私人DatabaseHelper DBHelper;
  私人SQLiteDatabase分贝;
  私人上下文的背景下;  //表列锻炼名
  公共静态最后弦乐KEY_DATE =日期;
  公共静态最后弦乐KEY_TIME =时间;
  公共静态最后弦乐KEY_NAME =姓名;
  公共静态最后弦乐KEY_PERIOD =周期;
  公共静态最后弦乐KEY_BURNEDCALS =Burned_Calories;  私有静态最后弦乐EXERCISE_TABLE_CREATE =
      创建表格练习(日期文本不为空,
      +时间文本不为空,名称文本不为空,
      +期间REAL NOT NULL,Burned_Calories REAL NOT NULL,
      +主键(日期,时间));;  公共DBAdapter(上下文ctxt){
    this.context = ctxt;
    DBHelper =新DatabaseHelper(背景);
  }  私有静态类DatabaseHelper扩展SQLiteOpenHelper {    DatabaseHelper(上下文的背景下){
      超(背景下,DB_NAME,空,DB_VERSION);
    }    公共无效的onCreate(SQLiteDatabase DB){
      尝试{
        db.execSQL(EXERCISE_TABLE_CREATE);
      }赶上(的SQLException E){
        e.printStackTrace();
      }
    }    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
      Log.w(TAG,从版本升级数据库
          + oldVersion +到+静态网页
          +,这将摧毁所有旧数据);
      db.execSQL(DROP TABLE IF EXISTS锻炼);
      的onCreate(DB);
    }    公共DBAdapter的open()抛出的SQLException {
      DB = DBHelper.getWritableDatabase();
      返回此;
    }    // ---关闭数据库---
    公共无效的close(){
      DBHelper.close();
    }    // ---插入运动信息,以练习表---
    众长SaveExecise(日期字符串,字符串时,字符串名称,浮动期,
        浮动BurnedCalories){
      ContentValues​​内容=新ContentValues​​();
      content.put(KEY_DATE,日期);
      content.put(KEY_TIME,时间);
      content.put(KEY_NAME,名);
      content.put(KEY_PERIOD,周期);
      content.put(KEY_BURNEDCALS,BurnedCalories);      返回db.insert(EXERCISE_TABLE_NAME,空,内容);
    }
  }
}

在这里我的地方中插入表格锻炼锻炼信息:

  save_exercise_btn =(按钮)findViewById(R.id.save_exercise_Btn);
save_exercise_btn.setOnClickListener(新View.OnClickListener(){
  公共无效的onClick(视图v){
    showSavingDialog();
    //保存名称,IME,演习在DB的burnedcals
    INT年,月,日;
    年= localCalendar.get(1);
    一个月= localCalendar.get(2)+ 1;
    天= localCalendar.get(5);
    字符串日期=年+/+月+/+天;
    DBAdapter dbAdapter =新DBAdapter(SelectedExerciseInfo.this);
    dbAdapter = dbAdapter.open();
    长D = dbAdapter.SaveExecise(日期,exercise_Time,ex_Name,分,burned_cals);
    dbAdapter.close();
    hideSavingDialog();
  }
});


解决方案

 私有静态最后弦乐EXERCISE_TABLE_CREATE =创建表练习(日期文本不为空,+
    时间文本不为空,名称文本不为空,+期间REAL不为空,
     Burned_Calories REAL NOT NULL,+主键(日期,时间));;

您已经把的的而不是表格。查询变得不正确的,没有创建表。改变和表会得到创建,然后可以添加数据

 私有静态最后弦乐EXERCISE_TABLE_CREATE =创建表的练习(日期文本不为空,+
    时间文本不为空,名称文本不为空,+期间REAL不为空,
     Burned_Calories REAL NOT NULL,+主键(日期,时间));;

勾选此code为DBAdapter:

 公共类DBAdapter
{
    私有静态最后弦乐DB_NAME =MYDB3131;
    私有静态最终诠释DB_VERSION = 1;
    私有静态最后弦乐EXERCISE_TABLE_NAME =锻炼;
    私有静态最后弦乐TAG =DBAdapter;
    私人DatabaseHelper DBHelper;
    私人SQLiteDatabase分贝;
    私人上下文的背景下;
    //表列锻炼名
    公共静态最后弦乐KEY_DATE =日期;
    公共静态最后弦乐KEY_TIME =时间;
    公共静态最后弦乐KEY_NAME =姓名;
    公共静态最后弦乐KEY_PERIOD =周期;
    公共静态最后弦乐KEY_BURNEDCALS =Burned_Calories;
    / *私有静态最后弦乐EXERCISE_TABLE_CREATE =创建表的练习(日期文本不为空,+
    时间文本不为空,名称文本不为空,+期间REAL NOT NULL,Burned_Calories REAL NOT NULL); ;* /
    私有静态最后弦乐EXERCISE_TABLE_CREATE =创建表的练习(日期文本不为空,+
    时间文本不为空,名称文本不为空,+期间REAL NOT NULL,Burned_Calories REAL NOT NULL,+主键(日期,时间)); ;公共DBAdapter(上下文ctxt)
    {this.context = ctxt;
    DBHelper =新DatabaseHelper(背景);
    }
    私有静态类DatabaseHelper扩展SQLiteOpenHelper
    {
        DatabaseHelper(上下文的背景下)
        {
            超(背景下,DB_NAME,空,DB_VERSION);
        }
        @覆盖
        公共无效的onCreate(SQLiteDatabase DB)
        {
            db.execSQL(EXERCISE_TABLE_CREATE);
            的System.out.println(hereeeee);
            }
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
        {
            Log.w(TAG,从版本升级数据库+ oldVersion +到+ NEWVERSION +,这将摧毁所有旧数据);
            db.execSQL(DROP TABLE IF EXISTS锻炼);
            的onCreate(DB);
        }
    }    // - 打开DB
    公共DBAdapter的open()抛出的SQLException
    {
        DB = DBHelper.getWritableDatabase();
        返回此;
    }    // ---关闭数据库---
    公共无效的close()
    {
        DBHelper.close();
    }
    // ---插入运动信息,以练习表---
    众长SaveExecise(日期字符串,字符串时,字符串名称,浮动期,浮动BurnedCalories)
    {
        ContentValues​​内容=新ContentValues​​();
        content.put(KEY_DATE,日期);
        content.put(KEY_TIME,时间);
        content.put(KEY_NAME,名);
        content.put(KEY_PERIOD,周期);
        content.put(KEY_BURNEDCALS,BurnedCalories);        返回db.insert(EXERCISE_TABLE_NAME,空,内容);
    }}

在code所使用的样本活动:

 公共类MYA延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    INT年,月,日;
    日历localCalendar = Calendar.getInstance();
    年= localCalendar.get(1);
 一个月= localCalendar.get(2)+ 1;
 天= localCalendar.get(5);
 字符串日期=年+/+月+/+天;
               DBAdapter dbAdapter =新DBAdapter(本);
              dbAdapter = dbAdapter.open();
 长D = dbAdapter.SaveExecise(日期,10034+100+,100F,100F);
               dbAdapter.close();
Toast.makeText(这个文+ D,Toast.LENGTH_SHORT).show();
}
}

I want to use SQLite and i have written the Database class and trying to insert the values in the database table but i am getting an Exception which is :

I/Database(958): sqlite returned: error code = 1, msg = no such table: Exercise   

and when i fixed the log it points to this statement :

long d= dbAdapter.SaveExecise(date, exercise_Time, ex_Name, minutes, burned_cals);

why this exception is raised ? i have read and tried solutions of such problems but the error still how i can solve it ? this is my code:

public class DBAdapter {

  private static final String DB_NAME = "MYDB";
  private static final int DB_VERSION = 1;
  private static final String EXERCISE_TABLE_NAME = "Exercise";
  private static final String TAG = "DBAdapter";
  private DatabaseHelper DBHelper;
  private SQLiteDatabase db;
  private Context context;

  // table Exercise columns name
  public static final String KEY_DATE = "Date";
  public static final String KEY_TIME = "Time";
  public static final String KEY_NAME = "Name";
  public static final String KEY_PERIOD = "Period";
  public static final String KEY_BURNEDCALS = "Burned_Calories";

  private static final String EXERCISE_TABLE_CREATE =
      "create tables Exercise (Date text not null  , "
      + "Time  text not null  ,Name  text not null,"
      + "Period  REAL not null, Burned_Calories  REAL  not null,"
      + " primary key(Date,Time ) );";

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

  private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
      super(context, DB_NAME, null, DB_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
      try {
        db.execSQL(EXERCISE_TABLE_CREATE);
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    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 Exercise");
      onCreate(db);
    }

    public DBAdapter open() throws SQLException {
      db = DBHelper.getWritableDatabase();
      return this;
    }

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

    // ---insert Exercise info to the Exercise table---
    public long SaveExecise(String date, String time, String name, float period,
        float BurnedCalories) {
      ContentValues content = new ContentValues();
      content.put(KEY_DATE, date);
      content.put(KEY_TIME, time);
      content.put(KEY_NAME, name);
      content.put(KEY_PERIOD, period);
      content.put(KEY_BURNEDCALS, BurnedCalories);

      return db.insert(EXERCISE_TABLE_NAME, null, content);
    }
  }
}

and here where i insert the exercise information in the Exercise table:

save_exercise_btn = (Button) findViewById(R.id.save_exercise_Btn);
save_exercise_btn.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    showSavingDialog();
    // save the name,ime,burnedcals of the exercise in the DB
    int year, month, day;
    year = localCalendar.get(1);
    month = localCalendar.get(2) + 1;
    day = localCalendar.get(5);
    String date = year + "/" + month + "/" + day;
    DBAdapter dbAdapter = new DBAdapter(SelectedExerciseInfo.this);
    dbAdapter = dbAdapter.open();
    long d = dbAdapter.SaveExecise(date, exercise_Time, ex_Name, minutes, burned_cals);
    dbAdapter.close();
    hideSavingDialog();
  }
});

解决方案

 private static final String EXERCISE_TABLE_CREATE ="create tables Exercise (Date text not null  , "+
    "Time  text not null  ,Name  text not null," + "Period  REAL not null, 
     Burned_Calories  REAL  not null," +" primary key(Date,Time ) );"  ;

You have put tables instead of table. The query becomes incorrect and no table is created. change it and the table would get created and data could then be added

 private static final String EXERCISE_TABLE_CREATE ="create table Exercise (Date text not null  , "+
    "Time  text not null  ,Name  text not null," + "Period  REAL not null, 
     Burned_Calories  REAL  not null," +" primary key(Date,Time ) );"  ;

Check this code for DBAdapter:

public class DBAdapter
{
    private static final String DB_NAME="MYDB3131";
    private static final int DB_VERSION= 1 ;
    private static final String EXERCISE_TABLE_NAME="Exercise";
    private static final String TAG = "DBAdapter";
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;
    private Context context;
    // table Exercise columns name
    public static final String KEY_DATE="Date";
    public static final String KEY_TIME="Time";
    public static final String KEY_NAME="Name";
    public static final String KEY_PERIOD="Period";
    public static final String KEY_BURNEDCALS="Burned_Calories";
    /*private static final String EXERCISE_TABLE_CREATE ="create table Exercise ( Date text not null  , "+
    "Time  text not null  ,Name  text not null," + "Period  REAL not null, Burned_Calories  REAL  not null );"  ;

*/
    private static final String EXERCISE_TABLE_CREATE ="create table Exercise (Date text not null  , "+
    "Time  text not null  ,Name  text not null," + "Period  REAL not null, Burned_Calories  REAL  not null," +" primary key(Date,Time ) );"  ;

public DBAdapter(Context ctxt)
    {      this.context=ctxt;
    DBHelper= new DatabaseHelper(context);
    }
    private static class DatabaseHelper extends SQLiteOpenHelper
    {      
        DatabaseHelper(Context    context)
        {
            super(context, DB_NAME, null, DB_VERSION);
        } 
        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(EXERCISE_TABLE_CREATE);
            System.out.println("hereeeee");
            }
        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 Exercise");                 
            onCreate(db);
        }
    }   

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

    //---closes the database---
    public void close()
    {
        DBHelper.close();
    }
    //---insert   Exercise info to the Exercise table---
    public long SaveExecise(String date ,String  time,String name ,float  period, float BurnedCalories)
    {
        ContentValues content = new ContentValues();
        content.put(KEY_DATE, date);
        content.put(KEY_TIME, time);
        content.put(KEY_NAME, name);
        content.put(KEY_PERIOD,period);
        content.put(KEY_BURNEDCALS, BurnedCalories);

        return db.insert(EXERCISE_TABLE_NAME, null, content);
    }}

The code for the sample Activity used :

public class MyA extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int year,month,day;
    Calendar localCalendar =  Calendar.getInstance();
    year = localCalendar.get(1);
 month = localCalendar.get(2)+1;
 day = localCalendar.get(5);
 String   date= year +"/"+month+"/"+day;
               DBAdapter    dbAdapter=new  DBAdapter(this);
              dbAdapter=dbAdapter.open();
 long d= dbAdapter.SaveExecise(date, 10034+"", 100+"", 100f, 100f);
               dbAdapter.close();
Toast.makeText(this, "text"+d, Toast.LENGTH_SHORT).show();
}
}

这篇关于如何解决没有这样的表,同时插入在源码数据库异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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