未知的sqlite db错误 [英] unknown sqlite db error

查看:114
本文介绍了未知的sqlite db错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试将数据插入我的sqlite数据库时,收到logcat错误编译时未知数据库logindata:".错误消息听起来很直接,但是我可以找到问题所在.在我的数据库类或正在插入数据的活动中.

when trying to insert data into my sqlite db I receive a logcat error "unknown database logindata: , while compiling:". the error message sounds straight forward but I can locate where the issue is. in my DB class or the activity which is inserting the data.

错误消息:

   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();
    }
     }

活动:

   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

logindata.db is not a valid table name, use something without the . e.g. just 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

这篇关于未知的sqlite db错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆