android - 未知的 sqlite 数据库错误

标签 android sqlite compiler-errors logcat

当尝试将数据插入我的 sqlite 数据库时,我收到一个 logcat 错误“未知数据库登录数据:,在编译时:”。错误消息听起来很简单,但我可以找到问题所在。在我的数据库类或插入数据的 Activity 中。

错误信息:

   unknown database logindata: , while compiling: create table if not exists logindata.db (_id integer primary key autoincrement,username text not null,password text not null);

数据库:

  public class LoginDB extends SQLiteOpenHelper {

//Table attributes
public static final String DATABASE_NAME = "logindata.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME_INFOTABLE = "credentials";

// Data attributes
public static final String COLUMN_NAME_USERNAME = "username";
public static final String COLUMN_NAME_PASSWORD = "password";

private SQLiteOpenHelper DBHelper;
private SQLiteDatabase db;



public LoginDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub      

        String sqlDataStore = "create table if not exists " +
        DATABASE_NAME + " ("+ BaseColumns._ID + " integer primary key autoincrement,"

                    + COLUMN_NAME_USERNAME + " text not null,"
                    + COLUMN_NAME_PASSWORD + " text not null);";

        db.execSQL(sqlDataStore);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if(oldVersion == 1 && newVersion == 2){
            //Upgrade the database
    }   

}

    public boolean Login(String username, String password) throws SQLException
    {
        Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_NAME + " WHERE username=? AND password=?",
                new String[]{username,password});
        if(mCursor !=null) {
            if(mCursor.getCount()>0)
            {
            return true;
            }
        }
            return false;
        }

    public void open() {
        // TODO Auto-generated method stub
        db = DBHelper.getWritableDatabase();
    }
    public void close() {
        DBHelper.close();
    }
     }

Activity :

   public class CredentialsActivity extends Activity implements OnClickListener{

   private Button lReg;
   private EditText lUname;
   private EditText lPword;

   private LoginDB loginDb = new LoginDB(CredentialsActivity.this);

   @Override
   protected void onCreate (Bundle savedInstanceState) {
   // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
   setContentView(R.layout.register);

   lReg = (Button)findViewById(R.id.reg_button);
   lReg.setOnClickListener(this);
   }

  public void onClick(View v) {

switch(v.getId()) {

case R.id.reg_button:
    lUname = (EditText)findViewById(R.id.reg_uname);
    lPword = (EditText)findViewById(R.id.reg_pswd);

    String uname = lUname.getText().toString();
    String pass = lPword.getText().toString();
    boolean invalid = false;

    if(uname.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Username Missing", Toast.LENGTH_SHORT).show();
    }else if(pass.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
    }
    if(invalid == false){
        addEntry(uname, pass);
        Intent i_register = new Intent(CredentialsActivity.this, LoginDB.class);
        startActivity(i_register);
        finish();
}}
}
    public void onDestroy() {
        super.onDestroy();
        loginDb.close();
    }

     public void addEntry(String uname, String pass){

     SQLiteDatabase db = loginDb.getWritableDatabase();
     ContentValues values = new ContentValues();
     values.put("username", uname);
     values.put("password", pass);

     try{
         db.insert(LoginDB.DATABASE_NAME, null, values);
         Toast.makeText(getApplicationContext(), "Saved! Please login now",            Toast.LENGTH_SHORT).show();
         }catch(Exception err){
      err.printStackTrace();
     }
    }
   }

最佳答案

logindata.db 不是有效的表名,请使用没有 的内容。 例如只是 logindata

public static final String DATABASE_NAME = "logindata.db";
public static final String TABLE_NAME = "logindata";

// ...

String sqlDataStore = "create table if not exists " +
TABLE_NAME + "...

// replace other places where you use DATABASE_NAME too

关于android - 未知的 sqlite 数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12014294/

相关文章:

java - 如何将自定义对象的 ArrayList 传递给新 Activity ?

java - 在 Android 中使用 ProgressbarDialog 下载文件

android - 估计android中的剩余电池时间

java - Android中为什么无法从SqlLiteDatabase读取数据

android - SQLiteOpenHelper onCreate()/onUpgrade() 何时运行?

java - Hibernate 映射列表 - 索引

gcc - 使用 c++0x 在 g++ 上出现 strdup 错误

c - 错误 : no previous prototype for 'check'

C++,错误 C2448 - 函数样式初始值设定项似乎是函数定义

android - Java.Lang.NoSuchMethodError : No non-static method "Landroid/content/Context;.getColorStateList(I)Landroid/content/res/ColorStateList;" 错误