2014-04-04 48 views
3

我有以下代碼! SelectTable目前不工作。我需要做的就是從數據庫中選擇一條特定記錄,並將其中的TextView更改爲所選記錄。
I.E., textView1.setText textView.setText 應將其設置爲所選記錄的名稱和位置! 請幫忙!如何選擇特定記錄並將其設置爲textView - Android SQlite

package com.example.sqlitedemo; 

public class MainActivity extends Activity { 

LinearLayout Linear; 
SQLiteDatabase mydb; 
private static String DBNAME = "PERSONS.db"; /
private static String TABLE = "MY_TABLE";  
TextView textView1,textView; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
    textView1 = (TextView)findViewById(R.id.textView1); 
    textView =(TextView) findViewById(R.id.textView2); 

      Toast.makeText(getApplicationContext(), "Creating table.", Toast.LENGTH_SHORT).show(); 

      dropTable();  // DROPPING THE TABLE. 
      createTable(); 

      insertIntoTable(); 
      selectTable(); 
      //showTableValues(); 

     } 

     public void createTable(){ 
      try{ 
      mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 
      mydb.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, NAME TEXT, PLACE TEXT);"); 
      mydb.close(); 
      }catch(Exception e){ 
       Toast.makeText(getApplicationContext(), "Error in creating table", Toast.LENGTH_LONG); 
      } 
     } 
     // THIS FUNCTION INSERTS DATA TO THE DATABASE 
     public void insertIntoTable(){ 
      try{ 
       mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 
       mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('CODERZHEAVEN','GREAT INDIA')"); 
       mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('ANTHONY','USA')"); 
       mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('SHUING','JAPAN')"); 
       mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('JAMES','INDIA')"); 
       mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('SOORYA','INDIA')"); 
       mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('MALIK','INDIA')"); 
       mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('myname','America')"); 
       mydb.close(); 
      }catch(Exception e){ 
       Toast.makeText(getApplicationContext(), "Error in inserting into table", Toast.LENGTH_LONG); 
      } 
     } 

     public void selectTable(){ 
      try{ 
      mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 
      String q = "SELECT * FROM " + TABLE + " WHERE ID = '1'"; 
      //String name = c.getString(c.getColumnName(0)); 
      //String place = c.getString(c.getColumnName(1)); 
      Cursor mCursor = mydb.rawQuery(q, null); 
      String name = mCursor.getString(0); 
      String place = mCursor.getString(1); 
      textView1.setText(name); 
      textView.setText(place); 

      mydb.close(); 
      }catch(Exception e){ 
       Toast.makeText(getApplicationContext(), "Error selecting", Toast.LENGTH_LONG); 
      } 

     } 

     public void dropTable(){ 
      try{ 
       mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 
       mydb.execSQL("DROP TABLE " + TABLE); 

       mydb.close(); 
      }catch(Exception e){ 
       Toast.makeText(getApplicationContext(), "Error encountered while dropping.", Toast.LENGTH_LONG); 
      } 
     } 
    } 

xml Layout : 


    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Medium Text" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Medium Text" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

     </LinearLayout> 
+0

應用程序崩潰,還是隻是沒有更新記錄?另外,字符串名稱和字符串位置是否有值,還是隻有空? – FMontano

回答

3

你必須創建表查詢,如下所示,

mydb.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, NAME TEXT, PLACE TEXT);"); 

這裏你的ID是整數格式,而在選擇查詢你訪問它爲ID =「1」,這是錯誤的。

要解決此問題,您需要進行以下更正。

  • 首先讓你的ID來自動增量如下,

    mydb.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, PLACE TEXT);"); 
    
  • 更改您的選擇查詢,如下所示,

    String q = "SELECT * FROM " + TABLE + " WHERE ID = 1"; 
    
  • 這種說法Cursor mCursor = mydb.rawQuery(q, null);寫後下面的語句,

    mCursor.moveToFirst(); 
    

由於您需要修改創建表查詢,我建議您刪除舊的應用程序並安裝新的應用程序,否則它將不會提供正確的輸出。

+0

謝謝!我將它改爲:String q =「SELECT * FROM」+ TABLE +「WHERE NAME ='ANTHONY'」;如何將此記錄的位置設置爲textview? – user3496326

+0

你已經完成了這部分語句'textView1.setText(name);' – Kedarnath

+0

你保存了我的一天! Freakin真棒!謝謝很多朋友:) – user3496326

相關問題