据库并从sqllite中创建和导出的资产文件夹android中读

据库并从sqllite中创建和导出的资产文件夹android中读

本文介绍了如何放置数据库并从sqllite中创建和导出的资产文件夹android中读取数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手,正在通过sqllite管理器0.8.1进行数据库登录,并且表用户包含两个字段用户名和密码,我已经填写了字段用户名和密码,现在导出数据库并放入资产文件夹中,现在我正在尝试从资产文件夹中读取数据库,如果匹配登录成功但不匹配登录不成功,则与用户输入匹配字段.我该怎么做,意味着从资产文件夹中读取数据库并与用户输入匹配.请帮助我.谢谢

I am new in android development,I am making a database login through sqllite manager 0.8.1,and table user contain two field username and password,i am alredy filled field username and password now export data base and put in asset folder,now i am tryng to read database from assets folder and match field with user input if matche login succesful and not match login unsuccesfull.how can i do this,means read database from assets folder and matched with user input.please help me.thanx

推荐答案

public class DataBaseHelper extends SQLiteOpenHelper {
    private SQLiteDatabase myDataBase;
    private final Context myContext;
    private static final String DATABASE_NAME = "YOURDBNAME";
    public final static String DATABASE_PATH = "/data/data/com.your.packagename/databases/";
    public static final int DATABASE_VERSION = 1;
    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.myContext = context;

    }




    //Create a empty database on the system
    public void createDatabase() throws IOException
    {

          boolean dbExist = checkDataBase();

          if(dbExist)
          {
                Log.v("DB Exists", "db exists");
                // By calling this method here onUpgrade will be called on a
                // writeable database, but only if the version number has been
                // bumped
                //onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
          }

          boolean dbExist1 = checkDataBase();
          if(!dbExist1)
          {
                this.getReadableDatabase();
                try
                {
                      this.close();
                      copyDataBase();
                }
                catch (IOException e)
                {
                      throw new Error("Error copying database");
                }
          }

    }
    //Check database already exist or not
    private boolean checkDataBase()
    {
          boolean checkDB = false;
          try
          {
                String myPath = DATABASE_PATH + DATABASE_NAME;
                File dbfile = new File(myPath);
                checkDB = dbfile.exists();
          }
          catch(SQLiteException e)
          {
          }
          return checkDB;
    }
    //Copies your database from your local assets-folder to the just created empty database in the system folder
    private void copyDataBase() throws IOException
    {

        InputStream mInput = myContext.getAssets().open(DATABASE_NAME);
        String outFileName = DATABASE_PATH + DATABASE_NAME;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[2024];
        int mLength;
        while ((mLength = mInput.read(mBuffer)) > 0) {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }
  //delete database
    public void db_delete()
    {
          File file = new File(DATABASE_PATH + DATABASE_NAME);
          if(file.exists())
          {
                file.delete();
                System.out.println("delete database file.");
          }
    }
  //Open database
    public void openDatabase() throws SQLException
    {
          String myPath = DATABASE_PATH + DATABASE_NAME;
          myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public synchronized void closeDataBase()throws SQLException
    {
          if(myDataBase != null)
                myDataBase.close();
          super.close();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         if (newVersion > oldVersion)
         {
               Log.v("Database Upgrade", "Database version higher than old.");
               db_delete();
         }

    }

}

并实施

DataBaseHelper dbhelper = new DataBaseHelper(getApplicationContext());

        db = dbhelper.getWritableDatabase();

为此:

Cursor cur;
    cur_Herbs = db.rawQuery("select * from TABLENAME where  name like '"
            + edit_text_name.gettext.tostring() + "'", null);

        if (cur.moveToFirst()) {
            do {
                int name = cur.getColumnIndex("name");
                int pwd= cur.getColumnIndex("pwd");
str_name = cur.getString(name).toString();
                str_pwd= cur.getString(ped).toString();
if(str_name.equals(edittext_uname.gettext.tostring()and str_pwd.equals(edittext_pwd.gettext.tostring()))
{

//code for if loginn
}
} while (cur_Herbs.moveToNext());


}

这篇关于如何放置数据库并从sqllite中创建和导出的资产文件夹android中读取数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:26