Android数据库帮助器插入方法

时间:2015-11-19 08:47:21

标签: android database

我已经获得了一个数据库助手类。想知道我为什么不能解决方法" insertCourse在我试图插入课程的其他活动中。这是数据库助手。

package ca.bcit.cst.comp3717.bcit;


import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class BCITDatabaseHelper
    extends SQLiteOpenHelper
{
    public static final String DB_NAME = "bcit";
    public static final int DB_VERSION = 1;
    public static final String COURSE_TABLE = "COURSE";
    public static final String COURSE_NAME = "NAME";
    public static final String COURSE_DESCRIPTION = "DESCRIPTION";

    public BCITDatabaseHelper(final Context context)
    {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(final SQLiteDatabase database)
    {
        database.execSQL("CREATE TABLE " + COURSE_TABLE + " (" +
                         "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                         COURSE_NAME + " TEXT, " +
                         COURSE_DESCRIPTION + " TEXT)");

        insertCourse(database, "COMP 3717", "Android");
       insertCourse(database, "COMP 3512", "C++");
       // insertCourse(database, "COMP 3711", "OOAD");
        //insertCourse(database, "COMP 3721", "Datacomm");
       // insertCourse(database, "COMP 3760", "Algorithms");
       // insertCourse(database, "COMP 3900", "Projects");
    }

    public void insertCourse(final SQLiteDatabase database,
                             final String name,
                             final String description)
    {
        final ContentValues courseValues;

        courseValues = new ContentValues();
        courseValues.put(COURSE_NAME,name);
        courseValues.put(COURSE_DESCRIPTION, description);
        database.insert(COURSE_TABLE, null, courseValues);
    }

    public void hi(){};
    @Override
    public void onUpgrade(final SQLiteDatabase database,
                          final int            oldVersion,
                          final int            newVersion)
    {
    }
}

这是我的其他活动,我正在尝试插入课程:

        try
        {
            final SQLiteOpenHelper helper;
            final SimpleCursorAdapter courseAdapter;

            helper   = new BCITDatabaseHelper(this);
            Intent i = getIntent();
            String [] names = i.getStringArrayExtra("names");
            String [] numbers = i.getStringArrayExtra("numbers");



            database = helper.getWritableDatabase();
            //for(int x = 0; x < names.length; x++){

               helper.insertCourse(database, "COMP 3717", "Android");
            //}
        }   

2 个答案:

答案 0 :(得分:1)

您必须明确地将helper对象转换为BCITDatabaseHelper

final SQLiteOpenHelper helper;更改为final BCITDatabaseHelper helper;

答案 1 :(得分:0)

try
    {
        final BCITDatabaseHelper helper;
        final SimpleCursorAdapter courseAdapter;

        helper   = new BCITDatabaseHelper(this);//Constructor to be called by the same boject of class
        Intent i = getIntent();
        String [] names = i.getStringArrayExtra("names");
        String [] numbers = i.getStringArrayExtra("numbers");



        database = helper.getWritableDatabase();
        //for(int x = 0; x < names.length; x++){

           helper.insertCourse(database, "COMP 3717", "Android");
        //}
    }