歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:使用已有的SQL數據庫

Android開發教程:使用已有的SQL數據庫

日期:2017/3/1 10:36:29   编辑:Linux編程

之前我們使用的數據庫都是在代碼裡面創建的。下面介紹一下如果使用外部已有的sql數據庫。

先用SQLite管理工具,sqliteadmin 具體操作很簡單,在這裡我就不詳細介紹的了,但有一個地方時候很值得注意的,就是用sqliteadmin創建數據庫的時候,數據庫保存的路徑不能是中文路徑,中文路徑會出現下面的錯誤提示:

650) this.width=650;">

昨天 12:13 上傳下載附件 (14.6 KB)

我在sqliteadmin 創建好數據庫StuDB,裡面的表如下:

650) this.width=650;">

將創建好的數據庫在DDMS中點擊650) this.width=650;">導入到data/data/程序的包名/

650) this.width=650;">

SQLiteTestActivity.java

  1. package com.lingdududu.test;
  2. import Android.app.Activity;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11. public class SQLiteTestActivity extends Activity {
  12. /** Called when the activity is first created. */
  13. private EditText studentText;
  14. private EditText teacherText;
  15. private Button queryBtn;
  16. SQLiteDatabase stuDb;
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. studentText = (EditText)findViewById(R.id.stu_name);
  22. teacherText = (EditText)findViewById(R.id.teacher_name);
  23. queryBtn = (Button)findViewById(R.id.query);
  24. queryBtn.setOnClickListener(new queryListener());
  25. }
  26. class queryListener implements OnClickListener{
  27. @Override
  28. public void onClick(View v) {
  29. //調用查詢方法
  30. query();
  31. stuDb.close();
  32. }
  33. }
  34. //查詢方法
  35. private void query() {
  36. //打開或者創建數據庫
  37. stuDb = SQLiteDatabase.openOrCreateDatabase("data/data/com.lingdududu.test/StuDB.s3db", null);
  38. try {
  39. String string =studentText.getText().toString();
  40. String sql = "Select sname from Student where snumber="+string;
  41. Cursor cursor = stuDb.rawQuery(sql,null);
  42. cursor.moveToFirst();
  43. teacherText.setText(cursor.getString(cursor.getColumnIndex("sname")));
  44. } catch (Exception e) {
  45. Toast.makeText(this, "請檢查輸入的學生學號是否正確", Toast.LENGTH_LONG).show();
  46. }
  47. }
  48. }

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/input_name"
  11. />
  12. <EditText
  13. android:id="@+id/stu_name"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. <Button
  18. android:id="@+id/query"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:text="開始查詢"
  22. />
  23. <TextView
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:text="@string/teacher_name"
  27. />
  28. <EditText
  29. android:id="@+id/teacher_name"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:editable="false"
  33. />
  34. </LinearLayout>

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, SQLiteTestActivity!</string>
  4. <string name="app_name">SQLiteTest</string>
  5. <string name="input_name">請輸入學生學號</string>
  6. <string name="teacher_name">該學生的姓名</string>
  7. </resources>

效果圖:

650) this.width=650;" height=197>

Copyright © Linux教程網 All Rights Reserved