SQLite(1)没有这样的表

时间:2014-05-07 10:40:29

标签: java android sqlite

当我运行我的Android应用程序时,我一直收到错误(1)没有这样的表userTable,据我所知,我正在创建表

TABLE_CREATE = "create table userTable("    
                + USER + " text not null, "  + PWD + " text not null, );";

我在这个项目中有两个其他数据库,并且工作正常,欢迎任何帮助。

package com.weightpro.db;

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

public class usrPwdDB {

public static final String USER = "userName";
public static final String PWD = "password";
public static final String TABLE_NAME = "userTable";
public static final String DATA_BASE_NAME = "userdatabase";
public static final String KEY_ROWID = "_id";
public static final int DB_VERSION = 2;
private static final String TABLE_CREATE = "create table userTable("    
            + USER + " text not null, "  + PWD + " text not null, );"; 

DBHelper WDBHelper;
Context mContext;
SQLiteDatabase db;

public usrPwdDB(Context mContext) {
    this.mContext = mContext;
    WDBHelper = new DBHelper(mContext);

}

 private static class DBHelper extends SQLiteOpenHelper
 {

    public DBHelper(Context context) {
        super(context,DATA_BASE_NAME, null, DB_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try{
        db.execSQL(TABLE_CREATE);
        }

        catch(SQLException e)
        {
            e.printStackTrace();
        }
    } 

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS userTable");
        onCreate(db);
    }


 }
public usrPwdDB open()
{
    db = WDBHelper.getWritableDatabase();
    return this;

}

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

public long insertInfo(String userName, String password){
    ContentValues content = new ContentValues();
    content.put(USER, userName);
    content.put(PWD, password);
    return db.insertOrThrow(TABLE_NAME, null, content);
}

public boolean getUserNameAndPassword(String userName, String Password) throws SQLException {

            Cursor mCursor =
            db.query(true, TABLE_NAME, new String[] {USER,
                    PWD},USER+"='"+userName+"' AND password='"+Password+"'", null,
                    null, null, null, null);

    if (mCursor.getCount() > 0)
    {
        return true;
    }
    return false;}

public Cursor returnData(){
    return db.query(TABLE_NAME, new String[] {USER, PWD},null,null,null,null,null);
}
}

3 个答案:

答案 0 :(得分:3)

从create table语法

中删除最后一个逗号,
private static final String TABLE_CREATE = 
    "create table userTable("    
    + USER + " text not null, "  
    + PWD + " text not null, );";    // remove this comma after not null

正确的语法应该是

private static final String TABLE_CREATE = 
    "create table userTable("    
    + USER + " text not null, "  
    + PWD + " text not null );";    

答案 1 :(得分:2)

你错了吗

 + USER + " text not null, "  + PWD + " text not null, );"; //remove , from last text not null

使用以下

更正SQL command
private static final String TABLE_CREATE = "create table userTable("    
        + USER + " text not null, "  + PWD + " text not null);" ;

答案 2 :(得分:0)

private static final String TABLE_CREATE = "create table userTable("    
        + USER + " text not null, "  + PWD + " text not null,);" ;

在“text not null”之后删除“,”并删除字符串中的“;”(分号),我认为不需要,

然后清除数据或卸载应用程序然后再次执行

即使创建表失败,我想下次SQLiteOpenHelper中的onCreate()只执行一次,所以你需要清除应用程序的数据

如果您对单独的表使用单独的类并使用相同的DB名称,那么第一个使用类中的所有表都可以,而其他表将不会创建,因为对于单个数据库onCreate()函数只能运行一个,即使每个单独的类都有onCreate(),

onCreate()函数在第一次调用onCreate()时在android文件系统的DB位置创建一个文件,如果onCreate再次调用它检查DB位置是否存在DBNAME文件并且如果退出则避免创建,我想这个是它背后的逻辑......

因此,为单个DB文件保留一个onCreate()函数,并在该函数中创建所有表