Mercy Mercy - 29 days ago 8
Android Question

How to create a two table in single Database in Android Application?

I have Created a database in Sqllite Android Application and I tried to add two tables in my Database, but I have problem to create that Database. First Table only Created. Can anyBody help me?

package com.android.cdtech;

import java.sql.SQLException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class saveData {
public static final String KEY_ROWID = "rowid"; public static final String KEY_DATE = "Date";public static final String KEY_NAME = "CustomerName";public static final String KEY_AMOUNT = "Amount";public static final String KEY_BANK = "Banks";
private static final String TAG = "DBAdapter";
public static final String KEY_BUSNAME="BusinessName";public static final String KEY_ADD="Address";public static final String KEY_CPERSON="ContactPerson";;
private static final String DATABASE_NAME = "EXPORTDETAILS";
private static final String DATABASE_TABLE = "Payment";
private static final String DATABASE_TABLE2 = "Customer";
private static final int DATABASE_VERSION = 1;
private static String DATABASE_CREATE =
"create table Payment (_id integer primary key autoincrement, "
+ "Date text not null,"+"CustomerName text not null,"+"Amount text not null,"+"Banks text not null);";

private static final String DATABASE_CREATECUS =
"create table Customer (_id integer primary key autoincrement, "
+ "BusinessName text not null,"+"Address text not null,"+"ContactPerson text not null,"+"PhoneNumber text not null,);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;



public saveData(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);
db.execSQL(DATABASE_CREATECUS);
}

@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 titles");
onCreate(db);
}
}
public saveData open()throws SQLException
{
db=DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}

public long insert(String Date,String CustomerName,String Amount,String Banks) {
// TODO Auto-generated method stub
ContentValues cv=new ContentValues();
cv.put(KEY_DATE,Date);
cv.put(KEY_NAME,CustomerName);
cv.put(KEY_AMOUNT,Amount);
cv.put(KEY_BANK,Banks);
return db.insert(DATABASE_TABLE, null,cv);
}
public long insertForm(String BusinessName ,String Address ,String ContactPerson) {
// TODO Auto-generated method stub
ContentValues cv=new ContentValues();
cv.put(KEY_BUSNAME,BusinessName);
cv.put(KEY_ADD,Address);
cv.put(KEY_CPERSON,ContactPerson);
}
public Cursor getlatlng()
{
Cursor latlngCursor = db.rawQuery("select * from " + DATABASE_TABLE,null);
if (latlngCursor != null)
{
latlngCursor.moveToFirst();
}
db.close();
return latlngCursor;
}
public Cursor order()
{
Cursor latlngCursor = db.rawQuery("select * from " + DATABASE_TABLE2,null);
if (latlngCursor != null)
{
latlngCursor.moveToFirst();
}
db.close();
return latlngCursor;
}
}



Error Code =1 No Such table for Customer

sai sai
Answer
use below two class

package Your 'packagename';


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "BistroDB";

    private static final int DATABASE_VERSION =1;

    // Database creation sql statement
    public static final String Table1= "create table table1name ("Your cloumns");";


    public static final String Table2 = "create table table2name ("Your cloumns");";




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

    // Method is called during creation of the database
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(table1);
       database.execSQL(table2);

    }

    // Method is called during an upgrade of the database, e.g. if you increase
    // the database version
    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        Log.w(DBHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS table1");
        database.execSQL("DROP TABLE IF EXISTS table2");

        onCreate(database);
    }


        public boolean deleteDatabase(Context context) {
            return context.deleteDatabase(DATABASE_NAME);
        }

}

Use below class to insert values into table

  package 'Your package name';



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

    public class DataBaseAdapter {

        // Database fields

        private Context context;
        private SQLiteDatabase database;
        private DBHelper dbHelper;

        public DataBaseAdapter(Context context) {
            this.context = context;
        }

        public DataBaseAdapter open() throws SQLException {
            dbHelper = new DBHelper(context);
            database = dbHelper.getWritableDatabase();
            return this;
        }

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


        public Cursor fetchAllTAble1data() {
            return database.query("MenuData", new String[] { "id", "Title",
                    "Image", "Description" }, null, null, null, null, null);
        }

        public Cursor fetchAllTable2data() {
            return database.query("RestaurantsData", new String[] {
                    "restaurant_id", "name", "phone", "email", "open_days",
                    "timing", "website", "loc_name", "street", "city", "longitude",
                    "latitude", "zip" }, null, null, null, null, null);
        }

        public void deleteTable(String tablename){
            database.execSQL("drop table if exists "+tablename+';');
        }
        public void createIndividualTable(String query){
            database.execSQL(query);
        }


        public void InsertTable1Data(TAble1 review) {
            ContentValues values = new ContentValues();
            values.put("Name", review.Name);
            values.put("Email", review.Email);
            values.put("Comment", review.Comment);
            values.put("Rating", review.Rating);

            database.insert("ReviewsData", null, values);

        }

        public void InsertTable2Data(TAble2 photos) {
            ContentValues values = new ContentValues();
            values.put("photo", photos.Photos);

            database.insert("PhotosData", null, values);

        }



        public ContentValues createContentValues(String category, String summary,
                String description) {
            ContentValues values = new ContentValues();

            return values;
        }
    }