数据库不是在sqlite中创建的

时间:2013-09-28 07:31:48

标签: android

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

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 个答案:

答案 0 :(得分:1)

试试这个..对你有帮助

package 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);
    }


        }

答案 1 :(得分:0)

请详细说明你的问题。你说“我想”告诉我们数据库没有创建。你检查了什么是100%肯定的?

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

答案 2 :(得分: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 )";

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

答案 3 :(得分:0)

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