0
package com.example.namedb;

import android.database.sqlite.SQLiteDatabase;
import android.database.SQLException;
import android.content.Context;

public class DataSource 
{
    private SQLiteDatabase db;
    private dbOpenHelper database_helper;

    public DataSource(Context context) 
    {
        database_helper = new dbOpenHelper(context);
    }


      public void open() throws SQLException 
      {
        db = database_helper.getWritableDatabase();
      }

      public void close() 
      {
          database_helper.close();
      }

}

我在 dbOpenHelper 上有一个错误,它说 dbOpenHelper 无法解析为一种类型。我不明白这个错误,我不知道如何解决它。伙计们,我需要你的帮助。请解释错误以及我该如何解决。

4

2 回答 2

1

你需要一个名为dbOpenHelper. 在您的班级中添加以下声明。它扩展SQLiteOpenHelper 并覆盖了它的方法。

    private static class dbOpenHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
         db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT, number TEXT, skypeId TEXT, address TEXT)"); //create table query
    }

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

此外,您的名称不遵循标准的 java 命名约定。您可以在SQLiteOpenHelper 此处找到相关的详细信息

于 2013-01-03T14:27:55.500 回答
0

请看一看。我已经提供了创建数据库的代码。与android中的公共类。

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.util.Log;

public class MyDB {
     public static final String KEY_ROWID = "_id";
        public static final String KEY_FIRSTNAME = "ID";
        public static final String KEY_LASTNAME = "CS";
        public static final String KEY_DESIGNATION = "CN";
        public static final String KEY_DN = "DN";  

        private static final String TAG = "MyDB";       
        private static final String DATABASE_NAME = "test.db";    
        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_CREATE_ValidateUser_DriverInfo =
            "create table tabletest1 (_id integer primary key autoincrement, "
            + "ID text not null, CS text not null,CN text not null,DN text not null);";

        private static final String DATABASE_CREATE_ValidateUser_TripInfo =
                "create table tabletest2 (_id integer primary key autoincrement, "
                + "TI text not null, PU text not null,LN text not null,FN text not null,Origin varchar not null,De text not null);";


        private Context context;
        private DatabaseHelper DBHelper;
        private SQLiteDatabase db;

        public MyDB(Context ctx) 
        {
            this.context = ctx;
            DBHelper = new DatabaseHelper(context);
        }

        private static class DatabaseHelper extends SQLiteOpenHelper 
        {
            DatabaseHelper(Context context) 
            {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) 
            {
                db.execSQL(DATABASE_CREATE_ValidateUser_DriverInfo);
                db.execSQL(DATABASE_CREATE_ValidateUser_TripInfo);
            }

            @Override
            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 Employee");
                onCreate(db);
            }
        }


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

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


        public long insertTitle(ContentValues initialValues,String TableName) 
        {


            return db.insert(TableName, null, initialValues);
        }




}

使用以下代码调用数据库并将值插入其中。

 ContentValues initialValues;

    MyDB mmdb=new MyDB(getBaseContext());            
    mmdb.open();
    initialValues = new ContentValues();
                                initialValues.put("ID", ID);
                                initialValues.put("CS", CS);
                                initialValues.put("CN", CN);
                                initialValues.put("DN", DN);



                            mmdb.insertTitle(initialValues,"ValidateUser_DriverInfo");  
            mmdb.close();   

这是创建它的示例和简单代码。

于 2013-01-03T14:29:17.790 回答