SQLiteOpenHelper的使用

android | 2016-01-07 23:54:18 | 阅读 1974 次 | 评论(0)
public class MainActivity extends AppCompatActivity {
public Button firstButton;
    private Button secondButton;
    private SQLiteDB dbHelper;
    private final String dbName="helperdb";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstButton= (Button) findViewById(R.id.button);
        firstButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  dbVersion(1);
                Toast.makeText(MainActivity.this,"this is myfirst ssqlite",Toast.LENGTH_LONG).show();
            }


        });
        secondButton= (Button) findViewById(R.id.button2);
        secondButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbVersion(2);
            }
        });


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
   public void dbVersion(int i) {
      dbHelper=new SQLiteDB(this,dbName,null,i) ;
       showMessage(dbHelper.showTable());
    }

    public  void showMessage(String s) {
        new AlertDialog.Builder(MainActivity.this).setTitle("Message").setMessage(s)
                .setNegativeButton("OK", new DialogInterface.OnClickListener()
                                         {
                                     public  void onClick(DialogInterface dialog, int which){}
                                  }).show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
/******************************************************/
public class SQLiteDB extends SQLiteOpenHelper{
    private static final int Version=1;
    public SQLiteDB(Context context, String name, SQLiteDatabase.CursorFactory factory,int version){
       super(context, name, factory, version);
    }

    public void onCreate(SQLiteDatabase db){
String sql="CREATE TABLE IF NOT EXISTS tableOne"+"(uname VARCHAR(50),pwd VARCHAR(50));";
        db.execSQL(sql);

    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
String sql="CREATE TABLE IF NOT EXISTS tabletwo"+"(uname VARCHAR(50),pwd VARCHAR(50));";
        db.execSQL(sql);
    }

    public String showTable(){
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.query("sqlite_master",new String[] {"name"},"type=?",new String[] {"table"},null,null,null,null);
        String tables="";
        if(cursor.getCount()!=0){
            cursor.moveToFirst();
            for (int i=0; i<cursor.getCount();i++){
                tables=tables+cursor.getString(0)+"\n";
                cursor.moveToNext();
            }
        }
return tables;

    }

}
文章评论,共0条
游客请输入验证码
浏览191157次
最新评论
  • yu1776151787:电子电路程序,强啊
  • qunxingw:普中,51 A6正常测试
  • zhouwenyuan:博主可以开发房产APP吗?
  • qunxingw:结合附件,在分表A或B...实验一下宏就理解了