我有以下代码!截至目前,selectTable无法工作。我只需要从数据库中选择一个特定的记录,并将该记录的textview更改为选中的记录。
即。,
textView1.settext文本
文本视图.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>
最佳答案
创建表查询如下所示,
mydb.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, NAME TEXT, PLACE TEXT);");
这里您的id是整数格式的,而在select查询中,您是以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";
在这个语句之后,写下下面的语句,
mCursor.moveToFirst();
由于您需要修改create table查询,我建议您删除旧的应用程序并安装新的应用程序,否则它将不会给出正确的输出。
关于android - 如何选择特定记录并将其设置为textView-Android SQlite,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22853035/