当我使用SQLite3检查时,为什么我的数据库表不显示?

时间:2015-12-09 14:10:11

标签: android sqlite android-sqlite

Android Studio中的SQLite存在问题。

我创建了一个数据库,但是我无法获得我创建的表格 这是问题

enter image description here

这是我的SQLiteOpenHelper班级

public class GreenSQLiteHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "greencampus.db";
    private static final int DB_VERSION = 2;

    //table USER
    public static final String USER_TABLE = "USER";
    public static final String COLLUMN_USERNAME = "USERNAME";
    public static final String COLLUMN_PASSWORD = "PASWORD";
    public static final String COLLUMN_NOREK=  "NOREK";
    public static final String COLLUMN_EMAIL = "EMAIL";
    public static final String CREATE_USER =
            "CREATE TABLE" + USER_TABLE + "("
                    + COLLUMN_NOREK + "TEXT PRIMARY KEY,"
            +COLLUMN_USERNAME + "TEXT,"
            + COLLUMN_PASSWORD + "TEXT,"
            + COLLUMN_EMAIL + "TEXT)";

    //table tapcash
    public static final String TAPCASH_TABLE = "TAPCASH";
    public static final String COLLUMN_FNAME = "FNAME";
    public static final String COLLUMN_LNAME = "LNAME";
    public static final String COLLUMN_SALDOT = "SALDOT";
    public static final String COLLUMN_SALDOR = "SALDOR";
    public static final String FOREIGN_KEY_NOREK = "USER_NOREK";
    public static final String CREATE_TAPCASH =
        "CREATE TABLE" + TAPCASH_TABLE +"("
                + BaseColumns._ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLLUMN_SALDOT + "TEXT, " +
                COLLUMN_SALDOR + "TEXT, " +
                COLLUMN_FNAME + "TEXT, " +
                COLLUMN_LNAME + "TEXT, " +
                FOREIGN_KEY_NOREK + "TEXT, " +
                "FOREIGN KEY(" + FOREIGN_KEY_NOREK + ") REFERENCES USER(NOREK)";


    //table bus
    public static final String BUS_TABLE = "BUS";
    public static final String COLLUMN_NOBUS = "NOBUS";
    public static final String COLLUMN_JADWAL = "JADWAL";
    public static final String FOREIGN_KEY_SHELTER ="SHELTER_ID";
    public static final String CREATE_BUS =
    "CREATE TABLE" + BUS_TABLE + "("
            + BaseColumns._ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLLUMN_NOBUS + "TEXT, " +
            COLLUMN_JADWAL + "TEXT, " +
            FOREIGN_KEY_SHELTER + "INTEGER, " +
            "FOREIGN KEY(" + FOREIGN_KEY_SHELTER + ") REFERENCES SHELTER(_ID)";

    //table shelter
    public static final String SHELTER_TABLE = "SHELTER";
    public static final String COLLUMN_NAMAS = "NAMA";
    public static final String COLLUMN_KOORDINATS = "KOORDINAT";
    public static final String CREATE_SHELTER =
        "CREATE TABLE" + SHELTER_TABLE + "("
                + BaseColumns._ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
        COLLUMN_NAMAS + "TEXT, " +
        COLLUMN_KOORDINATS + "TEXT) ";

    //table map
    public static final String MAP_TABLE = "MAP";
    public static final String COLLUMN_NAMAM = "NAMA MAP";
    public static final String COLLUMN_KOORDINATM = "KOORDINAT";
    public static final String CREATE_MAP =
        "CREATE TABLE" + MAP_TABLE + "("
                + BaseColumns._ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
        COLLUMN_NAMAM + "TEXT, " +
        COLLUMN_KOORDINATM + "TEXT) ";

    public GreenSQLiteHelper(Context context){
        super(context, DB_NAME, null, DB_VERSION );
        SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try{
            db.execSQL(CREATE_USER);
            db.execSQL(CREATE_TAPCASH);
            db.execSQL(CREATE_BUS);
            db.execSQL(CREATE_SHELTER);
            db.execSQL(CREATE_MAP);
        }
        catch (SQLException e){
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       try {
           db.execSQL("DROP TABLE IF EXIST " + USER_TABLE);
           db.execSQL("DROP TABLE IF EXIST " + TAPCASH_TABLE);
           db.execSQL("DROP TABLE IF EXIST " + BUS_TABLE);
           db.execSQL("DROP TABLE IF EXIST " + SHELTER_TABLE);
           db.execSQL("DROP TABLE IF EXIST " + MAP_TABLE);
           onCreate(db);
       }
       catch (SQLException e){
           e.printStackTrace();
       }
    }
}

我正在使用数据源类

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

public class GreenDataSource {
    private Context mContext;
    GreenSQLiteHelper mGreenSQLiteHelper;

    public GreenDataSource(Context context){
        mContext = context;
        mGreenSQLiteHelper = new GreenSQLiteHelper(context);
        SQLiteDatabase database = mGreenSQLiteHelper.getReadableDatabase();
        database.close();
    }
}

这是我的活动

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        GreenDataSource myDb =  new GreenDataSource(this);
    }
}

他们说SQLiteOpenHelper中的onCreate只运行一次。那我该怎么办?

请帮助我,我在截止日期

2 个答案:

答案 0 :(得分:0)

所有您的表创建命令缺少名称与数据类型和/或语句之间的必要空格。

他们需要像这样重写:

public static final String CREATE_USER =
        "CREATE TABLE " + USER_TABLE + "("
                + COLLUMN_NOREK + " TEXT PRIMARY KEY,"
        +COLLUMN_USERNAME + " TEXT,"
        + COLLUMN_PASSWORD + " TEXT,"
        + COLLUMN_EMAIL + " TEXT)";

//table tapcash
public static final String TAPCASH_TABLE = "TAPCASH";
public static final String COLLUMN_FNAME = "FNAME";
public static final String COLLUMN_LNAME = "LNAME";
public static final String COLLUMN_SALDOT = "SALDOT";
public static final String COLLUMN_SALDOR = "SALDOR";
public static final String FOREIGN_KEY_NOREK = "USER_NOREK";
public static final String CREATE_TAPCASH =
    "CREATE TABLE " + TAPCASH_TABLE +"("
            + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLLUMN_SALDOT + " TEXT, " +
            COLLUMN_SALDOR + " TEXT, " +
            COLLUMN_FNAME + " TEXT, " +
            COLLUMN_LNAME + " TEXT, " +
            FOREIGN_KEY_NOREK + " TEXT, " +
            "FOREIGN KEY(" + FOREIGN_KEY_NOREK + ") REFERENCES USER(NOREK))";


//table bus
public static final String BUS_TABLE = "BUS";
public static final String COLLUMN_NOBUS = "NOBUS";
public static final String COLLUMN_JADWAL = "JADWAL";
public static final String FOREIGN_KEY_SHELTER ="SHELTER_ID";
public static final String CREATE_BUS =
"CREATE TABLE " + BUS_TABLE + "("
        + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
        COLLUMN_NOBUS + " TEXT, " +
        COLLUMN_JADWAL + " TEXT, " +
        FOREIGN_KEY_SHELTER + " INTEGER, " +
        "FOREIGN KEY(" + FOREIGN_KEY_SHELTER + ") REFERENCES SHELTER(_ID))";

//table shelter
public static final String SHELTER_TABLE = "SHELTER";
public static final String COLLUMN_NAMAS = "NAMA";
public static final String COLLUMN_KOORDINATS = "KOORDINAT";
public static final String CREATE_SHELTER =
    "CREATE TABLE " + SHELTER_TABLE + "("
            + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
    COLLUMN_NAMAS + " TEXT, " +
    COLLUMN_KOORDINATS + " TEXT) ";

//table map
public static final String MAP_TABLE = "MAP";
public static final String COLLUMN_NAMAM = "NAMA MAP";
public static final String COLLUMN_KOORDINATM = "KOORDINAT";
public static final String CREATE_MAP =
    "CREATE TABLE " + MAP_TABLE + "("
            + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
    COLLUMN_NAMAM + " TEXT, " +
    COLLUMN_KOORDINATM + " TEXT) ";

答案 1 :(得分:0)

你的创建表字符串是错误的,它们缺少一些空格。例如,在用户表创建时:

public static final String CREATE_USER =
        "CREATE TABLE" + USER_TABLE + "("
                + COLLUMN_NOREK + "TEXT PRIMARY KEY,"
        +COLLUMN_USERNAME + "TEXT,"
        + COLLUMN_PASSWORD + "TEXT,"
        + COLLUMN_EMAIL + "TEXT)";

上面的代码将连接以下字符串:

public static final String CREATE_USER = "CREATE TABLEUSER(NOREKTEXT PRIMARY KEY,USERNAMETEXT,PASSWORDTEXT,EMAILTEXT)";

正确的解决方案是:

public static final String CREATE_USER =
        "CREATE TABLE " + USER_TABLE + " ("
                + COLLUMN_NOREK + " TEXT PRIMARY KEY,"
        +COLLUMN_USERNAME + " TEXT,"
        + COLLUMN_PASSWORD + " TEXT,"
        + COLLUMN_EMAIL + " TEXT)";

我希望你看到包含空格的区别。再次检查SQL脚本字符串,修复它们并尝试再次运行代码。