2014-04-04 33 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

相关问题