0

I am new to android development and have a database with two tables "users" and "item". I have tried to create a new table to the existing database amd updatet my database version number and placed the code to create the new table in "onUpgrade" method, but I get an error saying the table "politicians" which I have added the create string in onUpdate method does not exist. What have I done wrong. I suspect that my onUpdrade method is incorrect, how do I add a table to an existing database in Android studio using sqlite, I am an android beginner. I first check the version then run a create string in my onUpagrade method as this was what I found when I searched how to solve the problem

package com.swaziprocurement.pricecheckerapp;

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;

/**
 * Created by dict_lp on 9/3/2016.
 */
public class DbHelper extends SQLiteOpenHelper {
    public static final String TAG=DbHelper.class.getSimpleName();
    public static final String DB_NAME="my_app.db";
    public static final int DB_VERSION=5;
    public static final String USER_TABLE="users";
    public static final String COLUMN_ID="id";
    public static final String COLUMN_EMAIL="email";
    public static final String COLUMN_PASS="password";

    public static final String ITEM_TABLE="item";
    public static final String COLUMN_IDD="id";
    public static final String COLUMN_PNAME="name";
    public static final String COLUMN_PDESC="desc";
    public static final String COLUMN_PCODE="code";
    public static final String COLUMN_PPRICE="price";

    public static final String POL_TABLE="politicians";
    public static final String COLUMN_IDDD="id";
    public static final String COLUMN_POLNAME="name";
    public static final String COLUMN_POLID="polid";
    public static final String COLUMN_POLWORK="polwork";
    public static final String COLUMN_POLTEL="poltel";


    public static final String CREATE_TABLE_ITEMS="CREATE TABLE IF NOT EXISTS " + ITEM_TABLE + "("
            + COLUMN_IDD + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_PNAME + " TEXT,"
            + COLUMN_PDESC + " TEXT,"
            + COLUMN_PCODE + " TEXT,"
            + COLUMN_PPRICE + " TEXT);";

    public static final String CREATE_TABLE_POLS="CREATE TABLE IF NOT EXISTS " + POL_TABLE + "("
            + COLUMN_IDDD + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_POLNAME + " TEXT,"
            + COLUMN_POLID + " TEXT,"
            + COLUMN_POLWORK + " TEXT,"
            + COLUMN_POLTEL + " TEXT);";

    public static final String CREATE_TABLE_USERS="CREATE TABLE IF NOT EXISTS " + USER_TABLE + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_EMAIL + " TEXT,"
            + COLUMN_PASS + " TEXT);";
    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE_USERS);
        sqLiteDatabase.execSQL(CREATE_TABLE_ITEMS);





    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       // db.execSQL("DROP TABLE IF EXISTS "+ ITEM_TABLE );
       // db.execSQL(CREATE_TABLE_ITEMS);
       // db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
       // db.execSQL("DROP TABLE IF EXISTS "+ POL_TABLE );
       // db.execSQL(CREATE_TABLE_POLS);
       // onCreate(db);
        if (oldVersion<3){
            db.execSQL(CREATE_TABLE_POLS);
        }



    }
    public void addUser(String email, String password){
        SQLiteDatabase db=this.getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put(COLUMN_EMAIL,email);
        values.put(COLUMN_PASS,password);

        long id =db.insert(USER_TABLE,null,values);
        db.close();

        Log.d(TAG,"user inserted" +id);

    }
    public Cursor getInformation(SQLiteDatabase db){

        String projections[]={COLUMN_IDD,COLUMN_PNAME,COLUMN_PCODE,COLUMN_PPRICE};//note to self double check if this is correct

        Cursor cursor=db.query(ITEM_TABLE,projections,null,null,null,null,null);

        return cursor;


    }
    public void addItem(String name,String Description,String Code,String Price){
        SQLiteDatabase db=this.getReadableDatabase();

        ContentValues values=new ContentValues();
        values.put(COLUMN_PNAME,name);
        values.put(COLUMN_PDESC,Description);
        values.put(COLUMN_PCODE,Code);
        values.put(COLUMN_PPRICE,Price);

        long id=db.insert(ITEM_TABLE,null,values);
        db.close();
        Log.d(TAG,"Item inserted");

    }

    public void addPol(String name,String polid,String polwork, String poltel){

        SQLiteDatabase db=this.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put(COLUMN_POLNAME,name);
        values.put(COLUMN_POLID,polid);
        values.put(COLUMN_POLWORK,polwork);
        values.put(COLUMN_POLTEL,poltel);

        long id=db.insert(POL_TABLE,null,values);
        db.close();
        Log.d(TAG,"Item inserted");
    }
    public boolean getUser(String email, String pass){
        String selectQuery="select * from " + USER_TABLE + " where " +
                COLUMN_EMAIL + " = "+"'"+email+"'" + " and " + COLUMN_PASS + " = " + "'"+pass+"'";
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor =db.rawQuery(selectQuery,null);
        //move to first row
        cursor.moveToFirst();
        if(cursor.getCount()>0){
            return true;
        }
        cursor.close();
        db.close();
        return false;

    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rza
  • 19
  • 1
  • 12

0 Answers0