我有以下代码! SelectTable目前无法正常工作。我需要做的就是从数据库中选择一个特定的记录,并将其TextView更改为所选的记录。
即
textView1.setText
textView.setText
应将其设置为所选记录的名称和位置!
请帮助!
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 :(得分:3)
您已创建表查询,如下所示
mydb.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, NAME TEXT, PLACE TEXT);");
此处您的ID是INTEGER格式,而在选择查询中,您正在以Id =&#34; 1&#34;来访问它,这是错误的。
要纠正此问题,您需要进行以下更正。
首先将您的ID设为AUTOINCREMENT,如下所示,
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();
由于您需要修改create table查询,我建议您删除旧的应用程序并安装新的应用程序,否则将无法提供正确的输出。