升级sqlite数据库时没有表错误

时间:2017-10-10 10:39:39

标签: android database sqlite android-sqlite

我是Android开发新手,拥有一个包含两个表“users”和“item”的数据库。我试图创建一个新表到现有数据库amd updatet我的数据库版本号并放置代码以“onUpgrade”方法创建新表,但我得到一个错误说表“政客”我添加了创建onUpdate方法中的字符串不存在。我做错了什么我怀疑我的onUpdrade方法不正确,如何使用sqlite将表添加到Android studio中的现有数据库,我是一个android初学者。我首先检查版本,然后在我的onUpagrade方法中运行一个创建字符串,因为这是我在搜索如何解决问题时发现的

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;

    }
}

0 个答案:

没有答案