0

There is two database is already in my project and want to create a new database for some task but database is not created i think i am doing something wrong in my code please help me out this problem. Thanks.

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


}
3
  • @deep-u have given permisson in android mainfest.xml file for external storage. Commented Sep 28, 2013 at 7:34
  • yes i have given permission in menifest.xml Commented Sep 28, 2013 at 7:43
  • @deep-i have added my code i have sucessfully created database u just created like this... Commented Sep 28, 2013 at 7:47

4 Answers 4

1

Try like this ..more helpful for u

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


        }
Sign up to request clarification or add additional context in comments.

Comments

0

Please elaborate a bit more your problem. You said "I think" to tell us the database is not created. What did you check to be 100% sure about that?

Also, do not use "id" but "_id". It is already defined for you by BaseColumns._ID.

3 Comments

Ok just curious about what you did to check the database is not there. Did you try to write something an got an error? Did you try changing id to _id?
ya i write _id instead of id and i am checking in ddms->data->project package name. there two old database is there but new which i am creating is not there.
@deep-just delete your both database by command promt and created again new database
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 )";

Replace this code with your code. Hope its work.

1 Comment

0

Database onCreate method is called when the database is opened via getWritableDatabase....try doing it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.