Android學習-----如何使用sqlite對於後台資料交換,sqlite使用常式入門

來源:互聯網
上載者:User

標籤:



SQLite 這是一個非常流行的嵌入式資料庫。它支援 SQL 查詢,和只使用很少的記憶體。Android 在整合實施 SQLite,所以每 Android 應用程式能夠使用 SQLite 資料庫。對數熟悉 SQL 的開發人員來時。使用 SQLite 相當簡單。

能夠。因為 JDBC 不適合手機這樣的記憶體受限裝置。所以 Android 開發人員須要學習新的 API 來使用 SQLite。本文以一個注冊登入Demo簡介一下sqlite入門使用。

先上一下執行結果:(請忽略醜陋的介面~~)

以下貼上主要代碼,後面分析:

/** * 登入頁面的activity * @author D_xiao * */public class MainActivity extends Activity {SQLiteDatabase db;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button loginbtn = (Button)findViewById(R.id.loginbtn);Button regbtn = (Button)findViewById(R.id.regbtn);db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/android1.db3", null); //建立或開啟資料庫loginbtn.setOnClickListener(new OnClickListener(){public void onClick(View sourse){boolean flag = false;String userName = ((EditText)findViewById(R.id.userEditText)).getText().toString();String userPassword = ((EditText)findViewById(R.id.passwordEditText)).getText().toString();try{Cursor cursor = db.rawQuery("select * from users where name = ? and password = ?",new String[]{userName,userPassword});if(cursor.getCount()==0){Intent intentE = new Intent(MainActivity.this,ErrorActivity.class);startActivity(intentE);}else{Intent intentS = new Intent(MainActivity.this,HomeActivity.class);intentS.putExtra("name", userName);startActivity(intentS);}}catch(SQLiteException se){Intent intentE = new Intent(MainActivity.this,ErrorActivity.class);startActivity(intentE);}}});regbtn.setOnClickListener(new OnClickListener(){public void onClick(View sourse){Intent intentReg = new Intent(MainActivity.this,RegActivity.class);startActivity(intentReg);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
/** * 注冊Activity * @author D_xiao * */public class RegActivity extends Activity {SQLiteDatabase db;ListView listView;Button btn;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_reg);db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/android1.db3", null);final String a = this.getFilesDir().toString();Button regOK = (Button)findViewById(R.id.regOK);Button backtologin = (Button)findViewById(R.id.backtologin);regOK.setOnClickListener(new OnClickListener(){public void onClick(View sourse){String name = ((EditText)findViewById(R.id.name)).getText().toString();String password = ((EditText)findViewById(R.id.password)).getText().toString();TextView suc = (TextView)findViewById(R.id.suc);try{db.execSQL("insert into users values(null,?

,?)",new String[]{name,password});suc.setText("注冊成功。請點取消button返回到登入介面");}catch(SQLiteException se){db.execSQL("create table users(_id integer primary key autoincrement," +"name varchar(20) ,"+"password varchar(200))");db.execSQL("insert into users values(null,?,?)",new String[]{name,password});suc.setText("注冊成功。請點取消button返回到登入介面");}}});backtologin.setOnClickListener(new OnClickListener(){public void onClick(View sourse){Intent intent = new Intent(RegActivity.this,MainActivity.class);startActivity(intent);}});}public void onDestroy(){super.onDestroy();if(db!=null&&db.isOpen()){db.close();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


/** * 登入成功顯示首頁,能夠發微博 並顯示朋友圈 * @author D_xiao * */public class HomeActivity extends Activity {SQLiteDatabase db;ListView listView;Button btn;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_home);db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/android1.db3", null);listView = (ListView)findViewById(R.id.show);btn = (Button)findViewById(R.id.send);btn.setOnClickListener(new OnClickListener(){Cursor cursor = null;public void onClick(View sourse){String weibo = ((EditText)findViewById(R.id.newtext)).getText().toString();Intent intent = getIntent();String name = intent.getStringExtra("name");try{insertData(db,name,weibo);//select * from weibo3cursor = db.rawQuery("select * from weiboa", null);inflateList(cursor);}catch(SQLiteException se){//primary key autoincrementdb.execSQL("create table weiboa(_id integer primary key autoincrement," +"name varchar(20) ,"+"weibo varchar(200))");insertData(db,name,weibo);//查詢cursor = db.rawQuery("select * from weiboa", null);inflateList(cursor);}finally{//cursor.close();}}});}private void insertData(SQLiteDatabase db,String name,String weibo){//運行插入語句db.execSQL("insert into weiboa values(null,?,?)",new String[]{name,weibo});}private void inflateList(Cursor cursor){//填充SimpleCursorAdapterSimpleCursorAdapter adapter = new SimpleCursorAdapter(HomeActivity.this,R.layout.line,cursor,new String[]{"name","weibo"},new int[]{R.id.my_name,R.id.my_weibo},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);listView.setAdapter(adapter);}public void onDestroy(){super.onDestroy();if(db!=null&&db.isOpen()){db.close();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


/** * username或password錯誤跳轉 * @author D_xiao * */public class ErrorActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_error);Button backbtn = (Button)findViewById(R.id.Ebackbtn);backbtn.setOnClickListener(new OnClickListener(){public void onClick(View sourse){Intent intentBack = new Intent(ErrorActivity.this,MainActivity.class);startActivity(intentBack);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


通過此例,能夠看出來sqlite和sql server 等資料庫的差別聯絡:

sqlite沒有圖形介面,也不須要不論什麼的配置安裝開啟串連等等的操作,幾句簡單的語句就能夠完畢增刪改查操作。使用起來還是非常方便的。並且sqlite和sql server ,MySQL是有非常多相似的地方的。除了大多數查詢語句在sqlite裡面都能夠用以外,sqlite還有自己的api提供的方法進行查詢,這個以後再敘。並且運行語句也非常相似。

比方db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/android1.db3", null);  這句相當於sql server中的建立串連,所以在使用完以後要關閉串連,都是一樣的。

       Cursor cursor = db.rawQuery("select * from users where name = ? and password = ?",new String[]{userName,userPassword});這句中的cursor相當於sql server 中的resultSet結思集。

沒有圖形介面有一點還是比較麻煩的,就是不好操作查看資料表,必需要執行cmd查看,相對來說比較麻煩。請看下篇博文:http://blog.csdn.net/frightingforambition/article/details/24439981

完整Demo:

http://download.csdn.net/detail/u011250851/7248227





著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

Android學習-----如何使用sqlite對於後台資料交換,sqlite使用常式入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.