0

我的项目中已经有两个数据库,想要为某些任务创建一个新数据库,但没有创建数据库我认为我的代码做错了,请帮我解决这个问题。谢谢。

public class LikeShareDbHelper extends SQLiteOpenHelper{
    static final int version = 10;
    static final String dbName = "LikeShareDB";
    static final String LikeAndShare = "LikeAndShareTable";
    static final String coluId = "id";
    static final String coluTask = "Task_id";
    static final String coluLike = "TLike";
    static final String coluComment = "TComment";
    static final String coluTSahre = "Tshare";
    static final String coluMySelf = "mtself";

    private static final String DATABASE_CREATE = "CREATE TABLE "
            + LikeAndShare + " (" + coluId
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + coluTask + " TEXT, "
            + coluLike + " TEXT, " + coluComment + " TEXT, " + coluTSahre
            + " Text, " + coluMySelf + " TEXT );";

    SQLiteDatabase db;
    private LikeShareDbHelper dbHelper;

    public LikeShareDbHelper(Context context) {
        super(context, dbName, null, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + LikeAndShare);
        onCreate(db);

    }


    public LikeShareDbHelper open() throws SQLException {
        db = dbHelper.getWritableDatabase();
        // dbHelper.open();
        return this;
    }

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


}
4

4 回答 4

1

试试这样..对你更有帮助

包 com.databases;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DataBaseHelper extends SQLiteOpenHelper {

    Context mContext;

    private static final String DATABASE_NAME = "Personal_Assistant_Chronical.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_DETAIL = "newContact";

    private static final String COLUMN_PERSON_ID= "personID";
    private static final String COLUMN_PERSON_NAME= "person_name";
    private static final String COLUMN_MOBILE_NUMBER = "mobile_no";
    private static final String COLUMN_LANDLINE_NUMBER = "lanline_no";
    private static final String COLUMN_FAX_NUMBER = "fax_no";
    private static final String COLUMN_EMAIL_ID = "email_id";
    private static final String COLUMN_PERSONAL_ADDRESS = "personal_address";
    private static final String COLUMN_WORK_ADDRESS = "work_address";

    private static final String CREATE_DETAIL_TABLE = "create table if not exists "
            + TABLE_DETAIL
            + "("
            + COLUMN_PERSON_ID
            +" INTEGER primary key autoincrement, "
            + COLUMN_PERSON_NAME
            + " VARCHAR(100) NOT NULL UNIQUE, "
            + COLUMN_MOBILE_NUMBER
            + " VARCHAR(100) NOT NULL , "
            + COLUMN_LANDLINE_NUMBER
            + " VARCHAR(100) NOT NULL, "
            + COLUMN_FAX_NUMBER
            + " VARCHAR(100) NOT NULL, "
            + COLUMN_EMAIL_ID
            + " VARCHAR(100) NOT NULL, "
            + COLUMN_PERSONAL_ADDRESS
            + " VARCHAR(100) NOT NULL, "
            + COLUMN_WORK_ADDRESS
            + " VARCHAR(100) NOT NULL); "
            ;
    private static final String TABLE_PERSONAL = "newPersonal";

    private static final String COLUMN_DATE_ID= "dateID";
    private static final String COLUMN_DATE= "fate";
    private static final String COLUMN_DIARY= "diary";

    private static final String CREATE_PERSONAL_TABLE = "create table if not exists "
            + TABLE_PERSONAL
            + "("
            + COLUMN_DATE_ID
            +" INTEGER primary key autoincrement , "
            + COLUMN_DATE
            +" VARCHAR(100) NOT NULL UNIQUE, "
            + COLUMN_DIARY
            + " VARCHAR(100) NOT NULL); "

            ;   


    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_DETAIL_TABLE);
        db.execSQL(CREATE_PERSONAL_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        if (db != null)
            onCreate(db);
    }


        }
于 2013-09-28T07:39:25.423 回答
0

请详细说明您的问题。您说“我认为”是为了告诉我们数据库没有创建。你做了什么检查才能 100% 确定这一点?

另外,不要使用“id”而是“_id”。它已由 为您定义BaseColumns._ID

于 2013-09-28T07:38:35.350 回答
0

当通过 getWritableDatabase 打开数据库时,会调用数据库 onCreate 方法......尝试这样做。

于 2013-09-28T10:02:53.043 回答
0
private static final String DATABASE_CREATE = "CREATE TABLE "
            + LikeAndShare + " (" + coluId
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + coluTask + " TEXT, "
            + coluLike + " TEXT, " + coluComment + " TEXT, " + coluTSahre
            + " Text, " + coluMySelf + " TEXT )";

将此代码替换为您的代码。希望它的工作。

于 2013-09-28T07:58:56.070 回答