找不到android表

时间:2015-08-06 03:30:56

标签: android database sqlite

我是android新手。我尝试在我的应用程序中创建用户,但是当我运行应用程序时,我在日志中出现此错误:

no such table: user (code 1): , while compiling: SELECT Id, Groom_Name, Groom_Family, Groom_Birthday, Groom_Address, Groom_Email, Bride_Name, Bride_Family, Bride_Birthday, Bride_Address, Bride_Email, UserName, Password, Wedding_Date, Creator, Register_time, Last_Login_Time, Mobile_Number, Mobile_Number2, State, City, Couple_Photo, Groom_Photo, Bride_Photo, License_Number, City_code, Status, Privacy, Money, bride_thumb, groom_thumb, couple_thumb, Activation FROM user

这是我的助手班:

package com.saniyeh.db.openHelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by mghua on 8/5/15.
 */
public class UserOpenHelper extends BaseOpenHelper {
    public static final String TABLE_NAME = "user";
    public static final String COL_ID = "Id";
    public static final String COL_GROOM_NAME = "Groom_Name";
    public static final String COL_GROOM_FAMILY = "Groom_Family";
    public static final String COL_GROOM_BIRTHDAY = "Groom_Birthday";
    public static final String COL_GROOM_ADDRESS = "Groom_Address";
    public static final String COL_GROOM_EMAIL = "Groom_Email";
    public static final String COL_BRIDE_NAME = "Bride_Name";
    public static final String COL_BRIDE_FAMILY = "Bride_Family";
    public static final String COL_BRIDE_BIRTHDAY = "Bride_Birthday";
    public static final String COL_BRIDE_ADDRESS = "Bride_Address";
    public static final String COL_BRIDE_EMAIL = "Bride_Email";
    public static final String COL_USERNAME = "UserName";
    public static final String COL_PASSWORD = "Password";
    public static final String COL_WEDDING_DATE = "Wedding_Date";
    public static final String COL_CREATOR = "Creator";//male or female
    public static final String COL_REGISTER_TIME = "Register_time";
    public static final String COL_LAST_LOGIN_TIME = "Last_Login_Time";
    public static final String COL_MOBILE_NUMBER = "Mobile_Number";
    public static final String COL_MOBILE_NUMBER2 = "Mobile_Number2";
    public static final String COL_STATE = "State";
    public static final String COL_CITY = "City";
    public static final String COL_COUPLE_PHOTO = "Couple_Photo";
    public static final String COL_GROOM_PHOTO = "Groom_Photo";
    public static final String COL_BRIDE_PHOTO = "Bride_Photo";
    public static final String COL_LICENSE_NUMBER = "License_Number";
    public static final String COL_CITY_CODE = "City_code";
    public static final String COL_STATUS = "Status";
    //enum('active','semiactive','deactive','register')
    public static final String COL_PRIVACY = "Privacy";
    public static final String COL_MONEY = "Money";
    public static final String COL_BRIDE_THUMB = "bride_thumb";
    public static final String COL_GROOM_THUMB = "groom_thumb";
    public static final String COL_COUPLE_THUMB = "couple_thumb";
    public static final String COL_ACTIVATION = "Activation";

    private static final String CREATE_TABLE =
            "CREATE TABLE IF NOT EXISTS wed90_user (" +
                    COL_ID + "  INTEGER PRIMARY KEY AUTOINCREMENT," +
                    COL_GROOM_NAME + "  TEXT, " +
                    COL_GROOM_FAMILY + "  TEXT " +
                    COL_GROOM_BIRTHDAY + "  TEXT NOT NULL, " +
                    COL_GROOM_ADDRESS + "  TEXT NOT NULL, " +
                    COL_GROOM_EMAIL + "  TEXT NOT NULL, " +
                    COL_BRIDE_NAME + "  TEXT, " +
                    COL_BRIDE_FAMILY + "  TEXT, " +
                    COL_BRIDE_BIRTHDAY + "  TEXT NOT NULL, " +
                    COL_BRIDE_ADDRESS + "  TEXT NOT NULL, " +
                    COL_BRIDE_EMAIL + "  TEXT NOT NULL, " +
                    COL_USERNAME + "  TEXT NOT NULL, " +
                    COL_PASSWORD + "  TEXT NOT NULL, " +
                    COL_WEDDING_DATE + "  TEXT NOT NULL, " +
                    COL_CREATOR + "  TEXT NOT NULL, " +
                    COL_REGISTER_TIME + " TEXT  NOT NULL, " +
                    COL_LAST_LOGIN_TIME + "  TEXT NOT NULL, " +
                    COL_MOBILE_NUMBER + "  INTEGER NOT NULL, " +
                    COL_MOBILE_NUMBER2 + "  INTEGER NOT NULL, " +
                    COL_STATE + "  TEXT NOT NULL, " +
                    COL_CITY + " TEXT NOT NULL, " +
                    COL_COUPLE_PHOTO + "  TEXT NOT NULL, " +
                    COL_GROOM_PHOTO + "  TEXT NOT NULL, " +
                    COL_BRIDE_PHOTO + "  TEXT NOT NULL, " +
                    COL_LICENSE_NUMBER + "  INTEGER NOT NULL, " +
                    COL_CITY_CODE + "  INTEGER NOT NULL, " +
                    COL_STATUS + "  TEXT NOT NULL, " +
                    COL_PRIVACY + "  INTEGER NOT NULL, " +
                    COL_MONEY + "  INTEGER NOT NULL, " +
                    COL_BRIDE_THUMB + "  TEXT NOT NULL, " +
                    COL_GROOM_THUMB + "  TEXT NOT NULL, " +
                    COL_COUPLE_THUMB + "  TEXT NOT NULL, " +
                    COL_ACTIVATION + "  TEXT NOT NULL" +
                    ")";

    public UserOpenHelper(Context context) {
        super(context, "iwed", null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

}

这是我的数据源类:

package com.saniyeh.db.datasource;

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;

import com.saniyeh.db.entity.User;
import com.saniyeh.db.openHelper.UserOpenHelper;

/**
 * Created by mghua on 8/5/15.
 */
public class UserDatasource {
    SQLiteDatabase database;
    SQLiteOpenHelper dbhelper;
    public static final String LOG = "MGH";
    public static final String[] ALL_COLS = {
            UserOpenHelper.COL_ID,
            UserOpenHelper.COL_GROOM_NAME,
            UserOpenHelper.COL_GROOM_FAMILY,
            UserOpenHelper.COL_GROOM_BIRTHDAY,
            UserOpenHelper.COL_GROOM_ADDRESS,
            UserOpenHelper.COL_GROOM_EMAIL,
            UserOpenHelper.COL_BRIDE_NAME,
            UserOpenHelper.COL_BRIDE_FAMILY,
            UserOpenHelper.COL_BRIDE_BIRTHDAY,
            UserOpenHelper.COL_BRIDE_ADDRESS,
            UserOpenHelper.COL_BRIDE_EMAIL,
            UserOpenHelper.COL_USERNAME,
            UserOpenHelper.COL_PASSWORD,
            UserOpenHelper.COL_WEDDING_DATE,
            UserOpenHelper.COL_CREATOR,
            UserOpenHelper.COL_REGISTER_TIME,
            UserOpenHelper.COL_LAST_LOGIN_TIME,
            UserOpenHelper.COL_MOBILE_NUMBER,
            UserOpenHelper.COL_MOBILE_NUMBER2,
            UserOpenHelper.COL_STATE,
            UserOpenHelper.COL_CITY,
            UserOpenHelper.COL_COUPLE_PHOTO,
            UserOpenHelper.COL_GROOM_PHOTO,
            UserOpenHelper.COL_BRIDE_PHOTO,
            UserOpenHelper.COL_LICENSE_NUMBER,
            UserOpenHelper.COL_CITY_CODE,
            UserOpenHelper.COL_STATUS,
            UserOpenHelper.COL_PRIVACY,
            UserOpenHelper.COL_MONEY,
            UserOpenHelper.COL_BRIDE_THUMB,
            UserOpenHelper.COL_GROOM_THUMB,
            UserOpenHelper.COL_COUPLE_THUMB,
            UserOpenHelper.COL_ACTIVATION,
    };

    public UserDatasource(Context context) {
        dbhelper = new UserOpenHelper(context);
        database = dbhelper.getWritableDatabase();
    }

    public void open() {
        Log.i(LOG, "Database opened");
        dbhelper.getWritableDatabase();
    }

    public void close() {
        Log.i(LOG, "Database closed");
        dbhelper.close();
    }

    public User create(User user) {
        ContentValues contentValues = new ContentValues();

        contentValues.put(UserOpenHelper.COL_GROOM_NAME, user.getGroomName());
        contentValues.put(UserOpenHelper.COL_GROOM_FAMILY, user.getGroomFamily());
        contentValues.put(UserOpenHelper.COL_GROOM_BIRTHDAY, user.getGroomBirthday());
        contentValues.put(UserOpenHelper.COL_GROOM_ADDRESS, user.getGroomAddress());
        contentValues.put(UserOpenHelper.COL_GROOM_EMAIL, user.getGroomEmail());
        contentValues.put(UserOpenHelper.COL_BRIDE_NAME, user.getBrideName());
        contentValues.put(UserOpenHelper.COL_BRIDE_FAMILY, user.getBrideFamily());
        contentValues.put(UserOpenHelper.COL_BRIDE_BIRTHDAY, user.getBrideBirthday());
        contentValues.put(UserOpenHelper.COL_BRIDE_ADDRESS, user.getBrideAddress());
        contentValues.put(UserOpenHelper.COL_BRIDE_EMAIL, user.getBrideEmail());
        contentValues.put(UserOpenHelper.COL_USERNAME, user.getUsername());
        contentValues.put(UserOpenHelper.COL_PASSWORD, user.getPassword());
        contentValues.put(UserOpenHelper.COL_WEDDING_DATE, user.getWeddingDate());
        contentValues.put(UserOpenHelper.COL_CREATOR, user.getCreator());
        contentValues.put(UserOpenHelper.COL_REGISTER_TIME, user.getRegisterTime());
        contentValues.put(UserOpenHelper.COL_LAST_LOGIN_TIME, user.getLastLoginTime());
        contentValues.put(UserOpenHelper.COL_MOBILE_NUMBER, user.getMobileNumber());
        contentValues.put(UserOpenHelper.COL_MOBILE_NUMBER2, user.getMobileNumber2());
        contentValues.put(UserOpenHelper.COL_STATE, user.getState());
        contentValues.put(UserOpenHelper.COL_CITY, user.getCity());
        contentValues.put(UserOpenHelper.COL_COUPLE_PHOTO, user.getCouplePhoto());
        contentValues.put(UserOpenHelper.COL_GROOM_PHOTO, user.getGroomPhoto());
        contentValues.put(UserOpenHelper.COL_BRIDE_PHOTO, user.getBridePhoto());
        contentValues.put(UserOpenHelper.COL_LICENSE_NUMBER, user.getLicenseNumber());
        contentValues.put(UserOpenHelper.COL_CITY_CODE, user.getCityCode());
        contentValues.put(UserOpenHelper.COL_STATUS, user.getStatus());
        contentValues.put(UserOpenHelper.COL_PRIVACY, user.getPrivacy());
        contentValues.put(UserOpenHelper.COL_MONEY, user.getMoney());
        contentValues.put(UserOpenHelper.COL_BRIDE_THUMB, user.getBrideThumb());
        contentValues.put(UserOpenHelper.COL_GROOM_THUMB, user.getGroomThumb());
        contentValues.put(UserOpenHelper.COL_COUPLE_THUMB, user.getCoupleThumb());
        contentValues.put(UserOpenHelper.COL_ACTIVATION, user.getActivation());
        long insertId = database.insert(UserOpenHelper.TABLE_NAME, null, contentValues);
        user.setId(insertId);
        return user;
    }

    public Cursor getCurrentUser() {
        Cursor cursor = database.query(UserOpenHelper.TABLE_NAME, UserDatasource.ALL_COLS, null, null, null, null, null);
        return cursor;
    }
}

现在在MainActivity类中我想要getCurrentUser,如果不存在则调用LoginActivit:

package com.saniyeh.iwedding;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.saniyeh.db.datasource.UserDatasource;
import com.saniyeh.db.openHelper.UserOpenHelper;

public class MainActivity extends Activity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    CustomDrawerAdapter adapter;

    List<DrawerItem> dataList;
    SQLiteDatabase sqLiteDatabase;
    UserDatasource userDatasource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

try {
                    UserDatasource userDatasource = new UserDatasource(this);
                    userDatasource.open();
                    Cursor cursor = userDatasource.getCurrentUser();
                }catch (Exception e){
                    Log.i("MGH",e.getMessage());
                }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        userDatasource.close();
    }




}

1 个答案:

答案 0 :(得分:3)

我认为您的表名是wed90_user而不是user您创建了变量TABLE_NAME,但似乎您没有在

中使用它
private static final String CREATE_TABLE =
            "CREATE TABLE IF NOT EXISTS wed90_user (" + ...