热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SQLiteException:没有此类列:basic(代码1)-SQLiteException:nosuchcolumn:basal(code1)

ImworkingwithdatabasesandIhavethefollowingDataBaseHandlingclass:我正在使用数据库,我有下面的数据库处理类:p

I'm working with databases and I have the following DataBaseHandling class:

我正在使用数据库,我有下面的数据库处理类:

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 4;

// Database Name
private static final String DATABASE_MEASURES = "measuresDataBase";

// Measures table name
private static final String TABLE_MEASURES = "measures";

// Measures Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_DATE = "date";
private static final String KEY_TIME_HOUR = "timeHour";
private static final String KEY_TIME_MINUTE = "timeMinute";
private static final String KEY_BE_INTAKE = "be_intake";
private static final String KEY_GLUCOSE = "glucose";
private static final String KEY_BOLUS = "bolus";
private static final String KEY_BASAL = "basal";

public DatabaseHandler(Context context) {
    super(context, DATABASE_MEASURES, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
            + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
            + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
            + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
            + " REAL NOT NULL," + KEY_BASAL + "REAL NOT NULL," + ")";

    db.execSQL(CREATE_MEASURES_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEASURES);

    // Create tables again
    onCreate(db);
}

// Adding new contact
public void addMeasure(Measures m) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_DATE, m.getDate());
    values.put(KEY_TIME_HOUR, m.getTimeHour());
    values.put(KEY_TIME_MINUTE, m.getTimeMinute());
    values.put(KEY_BE_INTAKE, m.getBolus());
    values.put(KEY_GLUCOSE, m.getGlucose()); // Contact Name
    values.put(KEY_BOLUS, m.getBolus()); // Contact Phone Number
    values.put(KEY_BASAL, m.getBasal());

    // Inserting Row
    db.insert(TABLE_MEASURES, null, values);
    db.close(); // Closing database connection
}

public Measures getLastMeasure(String date) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_MEASURES, new String[] { KEY_ID,
            KEY_DATE, KEY_TIME_HOUR, KEY_TIME_MINUTE, KEY_BE_INTAKE,
            KEY_GLUCOSE, KEY_BOLUS, KEY_BASAL }, KEY_DATE + "=?",
            new String[] { date }, null, null,
            KEY_TIME_HOUR + " DESC, " + KEY_TIME_MINUTE + " DESC", "1");
    if (cursor != null)
        cursor.moveToFirst();

    Measures m = new Measures(Long.parseLong(cursor.getString(0)), // id
            cursor.getString(1), // date
            Integer.parseInt(cursor.getString(2)), // timeHour
            Integer.parseInt(cursor.getString(3)), // timeMinute
            Double.parseDouble(cursor.getString(4)), // BE intake
            Double.parseDouble(cursor.getString(5)), // glucose
            Double.parseDouble(cursor.getString(6)), // bolus
            Double.parseDouble(cursor.getString(7)) // basal

    );
    // return m
    return m;
}

// Getting All Measures
public List getAllMeasures() {
    List measureList = new ArrayList();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_MEASURES;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Measures m = new Measures();
            m.setId(Long.parseLong(cursor.getString(0)));
            m.setDate(cursor.getString(1));
            m.setTimeHour(Integer.parseInt(cursor.getString(2)));
            m.setTimeMinute(Integer.parseInt(cursor.getString(3)));
            m.setBe_intake(Double.parseDouble(cursor.getString(4)));
            m.setGlucose(Double.parseDouble(cursor.getString(5)));
            m.setBolus(Double.parseDouble(cursor.getString(6)));
            m.setBasal(Double.parseDouble(cursor.getString(7)));
            // Adding measure to list
            measureList.add(m);
        } while (cursor.moveToNext());
    }

    // return contact list
    return measureList;
}

The logcat shows:

logcat显示:

android.database.sqlite.SQLiteException: no such column: basal (code 1):, while compiling: SELECT id, date, timeHour, timeMinute, be_intake, glucose, bolus, basal FROM mesaures WHERE date=? ORDER BY timeHour DESC LIMIT 1

android.database.sqlite。SQLiteException:没有此类列:basic(代码1):,编译时:选择id, date, timeHour, timeMinute, be_acceptance,葡萄糖,bolus, basic FROM mesaures WHERE date=?限时订货

I would thank all your help

谢谢你的帮助

My Logcat (after resolving the first problem)

我的Logcat(解决第一个问题后)

08-27 14:34:51.316: E/AndroidRuntime(772): FATAL EXCEPTION: main 08-27 14:34:51.316: E/AndroidRuntime(772): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 08-27 14:34:51.316: E/AndroidRuntime(772): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 08-27 14:34:51.316: E/AndroidRuntime(772): at com.example.t1diabetes.DatabaseHandler.getLastMeasure(DatabaseHandler.java:92) 08-27 14:34:51.316: E/AndroidRuntime(772): at com.example.t1diabetes.Mymeasures.calculateBasal(Mymeasures.java:263) 08-27 14:34:51.316: E/AndroidRuntime(772): at com.example.t1diabetes.Mymeasures.access$1(Mymeasures.java:262) 08-27 14:34:51.316: E/AndroidRuntime(772): at com.example.t1diabetes.Mymeasures$1.onClick(Mymeasures.java:99) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.view.View.performClick(View.java:4084) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.view.View$PerformClick.run(View.java:16966) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.os.Handler.handleCallback(Handler.java:615) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.os.Handler.dispatchMessage(Handler.java:92) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.os.Looper.loop(Looper.java:137) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.app.ActivityThread.main(ActivityThread.java:4745) 08-27 14:34:51.316: E/AndroidRuntime(772): at java.lang.reflect.Method.invokeNative(Native Method) 08-27 14:34:51.316: E/AndroidRuntime(772): at java.lang.reflect.Method.invoke(Method.java:511) 08-27 14:34:51.316: E/AndroidRuntime(772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 08-27 14:34:51.316: E/AndroidRuntime(772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-27 14:34:51.316: E/AndroidRuntime(772): at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(772):致命例外:main 08-27 14:34:51.316: E/AndroidRuntime(772): android.database。每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑每日一笑(字幕)(字幕版)(字幕版)(字幕版)(字幕版)(字幕版)(字幕版)(字幕版)(字幕版)(字幕版)(字幕版)(字幕版)在android.app.ActivityThread.main(ActivityThread.java:4745) 08-27 14:34:51.316: E/AndroidRuntime(772): at java.lang.reflect.Method。每日翻译(08/27):每日翻译(08/27):每日翻译(772):在爪哇。主要(本地方法)

5 个解决方案

#1


5  

Use a space after KEY_BASAL column . As your table column name was not correct , the table was created without this column. So after modifying code re-install your database or change version.Then you will get correct table with columns

在key_basic列之后使用空格。由于您的表列名称不正确,因此在没有这个列的情况下创建了表。因此,修改代码后,重新安装数据库或更改版本。然后,您将得到具有列的正确的表。

String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
            + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
            + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
            + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
            + " REAL NOT NULL," + KEY_BASAL + " REAL NOT NULL" + ")";


if (cursor != null && cursor .getCount()>0){

        cursor.moveToFirst();

///

}

#2


2  

There is a problem that you have missed a space at the Key_basal and also placed an extra comma at the end of your onCreate method. Your code :

有一个问题是您错过了key_basic的一个空格,并在onCreate方法的末尾添加了一个额外的逗号。你的代码:

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
        + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
        + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
        + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
        + " REAL NOT NULL," + KEY_BASAL + "REAL NOT NULL," + ")";

db.execSQL(CREATE_MEASURES_TABLE);
}

It should be :

应该是:

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
        + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
        + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
        + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
        + " REAL NOT NULL," + KEY_BASAL + " REAL NOT NULL" + ")";

db.execSQL(CREATE_MEASURES_TABLE);
}

#3


1  

Autoincrement KEY_ID or put some data to KEY_ID as well as put your full log cat here:

自动增加KEY_ID或将一些数据放到KEY_ID中,并将完整的日志cat放在这里:

String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_DATE + " TEXT,"
        + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
        + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
        + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
        + " REAL NOT NULL," + KEY_BASAL + " REAL NOT NULL" + ")";

#4


1  

Remove , End of Query

删除,查询结束

KEY_BASAL + "REAL NOT NULL," + ")";
                          ^
                          | Here

And Use Space For Concat Strings

并使用空间来处理字符串。

KEY_BASAL + "REAL NOT NULL," + ")";
             ^
             | Here

GoodLuck

古德勒克

#5


0  

I got this error while using junit tests. And my error was simple but evil.

我在使用junit测试时犯了这个错误。我的错误很简单,但却是邪恶的。

I mixed getContext() and getMockConetext().

我混合了getContext()和getMockConetext()。


推荐阅读
  • Hibernate入门指南:单表数据库操作详解
    本文介绍了Hibernate作为全面的ORM框架的基础知识,并详细讲解了在MyEclipse环境中配置Hibernate以及进行基本的数据库单表操作的方法,包括增删改查等常见操作。 ... [详细]
  • 使用EF Core在.Net Core控制台应用中操作SQLite数据库
    本文介绍如何利用Visual Studio 2019和Windows 10环境,通过Entity Framework Core(EF Core)实现对SQLite数据库的读写操作。项目源代码可从百度网盘下载。 ... [详细]
  • 本文详细介绍如何在Spring Boot项目中集成和使用JPA,涵盖JPA的基本概念、Spring Data JPA的功能以及具体的操作步骤,帮助开发者快速掌握这一强大的持久化技术。 ... [详细]
  • 抽象工厂模式 c++
    抽象工厂模式包含如下角色:AbstractFactory:抽象工厂ConcreteFactory:具体工厂AbstractProduct:抽象产品Product:具体产品https ... [详细]
  • 计算机视觉中的行为识别:步态分析的ReID应用
    本文介绍了一项基于步态的行为识别(ReID)项目,通过步态特征实现个体身份的准确识别。文中展示了项目的基本原理、特征提取方法及其实现效果。 ... [详细]
  • 本文通过对OkHttp源码的详细解读,旨在帮助读者理解其核心执行流程,特别是同步与异步请求的处理方式。文中不仅涵盖了基本的使用示例,还深入探讨了OkHttp的核心功能——拦截器链的工作原理。 ... [详细]
  • 使用URLHttpConnection获取并展示图片至ImageView的方法
    本文介绍如何通过URLHttpConnection方式从网络加载图片,并将其显示在Android应用的ImageView组件上。包括布局文件和Java代码的具体实现。 ... [详细]
  • JobScheduler5.0源码分析
    0.JobScheduler执行代码mJobScheduler(JobScheduler)getSystemService(Context.JOB_SCHEDULER_SERVICE); ... [详细]
  • 使用Python模拟登录教务系统抓取成绩并分析存储
    本文详细介绍如何使用Python编程语言模拟登录学校教务系统,抓取学生的成绩信息,并进行数据分析和可视化处理,最终将数据存储到MySQL数据库中。 ... [详细]
  • 深入解析 Android 中的 ActivityGroup 实现
    本文详细探讨了如何在 Android 应用中使用 ActivityGroup 来实现类似微博客户端主界面的效果,并分析了 TabActivity 的局限性,推荐使用更为灵活的 ActivityGroup 方案。 ... [详细]
  • 本文讨论了在处理分页数据时常见的低级错误,并提供了优化后的代码示例,以减少重复代码并提高可读性和维护性。 ... [详细]
  • 在Android Studio中查看SQLite数据库
    原来查看数据库内容,我们一般都是将数据库文件从手机导出,再用专门的软件打开查看,比较繁琐。最近发现了一个比较方便的方法:使用工具stetho。使用方式在gradle中配置depen ... [详细]
  • 本文总结了几个常用的Android开发技巧,包括检测设备上是否安装特定应用、获取应用的版本名称、设置状态栏透明以及如何从一个应用跳转至另一个应用的方法。 ... [详细]
  • 设计模式系列-原型模式
    一、上篇回顾上篇创建者模式中,我们主要讲述了创建者的几类实现方案,和创建者模式的应用的场景和特点,创建者模式适合创建复杂的对象,并且这些对象的每个组成部分的详细创建步骤可以是动态的变化的,但 ... [详细]
  • IhaveonedoubtinSqlite.dteTimeDataTypeisVarchar(200)inTablestructure.Iwanttogetresult ... [详细]
author-avatar
一起去钓鱼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有