简体   繁体   English

我的SQLite数据库中的上下文有问题,但我找不到它。 (使用SQLiteAssetHelper)

[英]There is something wrong with my context in my SQLite database, but I can't find it. (With SQLiteAssetHelper)

I have some troubles with my Database activity. 我的数据库活动有些麻烦。

I have my database (Voedsel.rar), in assets-databases-Voedsel.rar. 我有我的数据库(Voedsel.rar),在assets-databases-Voedsel.rar。

I also have one SQLView activity, which should be able to view the database. 我还有一个SQLView活动,它应该能够查看数据库。 Also, I have the SQLite activity (for writing new products to it.), and my regular Database activty. 此外,我有SQLite活动(用于编写新产品。)和我的常规数据库活动。 I want to write to the database, and to view the database. 我想写入数据库,并查看数据库。

There are 3 columns, Product, Eenheid (translated it means 'unit'), and Kcal (the amount of kilocalories in that specific units of that specific food-product.). 有3列,Product,Eenheid(翻译为'单位')和Kcal(特定食品的特定单位中的千卡数量)。 Hopefully you all understand me. 希望你们都理解我。 :) :)

I'll add my activities. 我会添加我的活动。 If there is need of the layout files, don't hesitate to ask. 如果需要布局文件,请不要犹豫。

So, the first question is: 所以,第一个问题是:

How can I fix my error in line 46, (see comment!)...? 如何在第46行修复错误,(见注释!)......?

Is there someting wrong with the Context? 上下文有什么问题吗?

my Database activity: 我的数据库活动:

    package com.jacob.eindproject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import java.sql.*;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class Database extends SQLiteAssetHelper {

    public static final String KEY_PRODUCT = "Product";
    public static final String KEY_EENHEID = "Eenheid";
    public static final String KEY_KCAL = "Kcal";

    private static final String DATABASE_NAME = "Voedsel";
    private static final String DATABASE_TABLE = "Voeding";
    private static final int DATABASE_VERSION = 1;

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

    private static class DbHelper extends SQLiteAssetHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

    }

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



    public Database(Context ourContext){ 

//Here is my error. It says: 
//Implicit super constructor 
//SQLiteAssetHelper() is undefined. Must explicitly invoke another constructor

        Context = ourContext;
    }

    public Database open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;        
    }

    public void close() {

    ourHelper.close();
}



    public long createEntry(String product, String kcal, String eenheid) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_PRODUCT, product);
        cv.put(KEY_EENHEID, eenheid);
        cv.put(KEY_KCAL, kcal);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_PRODUCT, KEY_EENHEID, KEY_KCAL};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iProduct = c.getColumnIndex(KEY_PRODUCT);
        int iEenheid = c.getColumnIndex(KEY_EENHEID);
        int iKcal = c.getColumnIndex(KEY_KCAL);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iProduct) + " " + c.getString(iEenheid) + " " + c.getString(iKcal) + "\n";


        }

        return result;
    }

    public void close(Database database) {
        // TODO Auto-generated method stub

    }



}

You need to call the super constructor. 你需要调用超级构造函数。 An example from this site is the following: 此站点的示例如下:

super(context, DATABASE_NAME, null, DATABASE_VERSION);

It looks like the addition of this line as the first line in your constructor should remove the error. 看起来添加此行作为构造函数中的第一行应该删除错误。

public Database(Context context){ 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

Secondly, you seem to have an inner SQLiteHelp. 其次,你似乎有一个内在的SQLiteHelp。 I can't imagine why that's needed, so I would just remove it. 我无法想象为什么需要它,所以我会删除它。

private static class DbHelper extends SQLiteAssetHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

}

Lastly, I would call your class something different than Database. 最后,我会把你的课程称为与数据库不同的东西。 I think you might be confusing classes somewhere, which could be responsible for your issue. 我认为你可能会在某个地方混淆课程,这可能是你的问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的Java代码出现错误,但看不到它有什么问题。 救命? - I'm getting an error in my Java code but I can't see whats wrong with it. Help? 我不能在不公开私有字段的情况下对课程进行单元测试-我的设计有问题吗? - I can't unit test my class without exposing private fields — is there something wrong with my design? 如何在不访问Context的情况下从线程更新Android SQLite数据库? - How can I update my Android SQLite database from a thread without access to the Context? 我的 JDBC 有问题,但我不知道为什么 - There's something wrong with my JDBC, but I don't know why 如果出现问题,如何返回程序的开头? - How can I return to the beginning of my program if something goes wrong? 我的程序在Android上的sqlite数据库中找不到表 - My program can not find tables in sqlite database on Android 我的算法有问题 - I have got something wrong in my algorithm Android Eclipse无法读取我的SQLite数据库 - Android Eclipse can't read my SQLite Database 为什么我不能在Android的SQLite数据库中添加这些默认值? - Why can't I add these default values in my SQLite database in Android? 为什么我不能使用SQLITE在数据库中插入新数据? - Why I can't insert new data in my database using SQLITE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM