public class MainActivity extends Activity {
String fname,lname,email;
SQLiteDatabase db;
TableRow tableRow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname VARCHAR,lname VARCHAR,email VARCHAR);");
}
public void data(View view)
{
EditText edittext1=(EditText )findViewById(R.id.firstname);
EditText edittext2=(EditText )findViewById(R.id.lastname);
EditText edittext3=(EditText )findViewById(R.id.email);
fname=edittext1.getText().toString();
lname=edittext2.getText().toString();
email=edittext3.getText().toString();
db.execSQL("INSERT INTO Student VALUES('"+fname+"','"+lname+"','"+email+"');");
}
public void showdata(View view)
{
Cursor c=db.rawQuery("SELECT * from Student", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView,textView1,textView2,textView3,textView4,textView5;
tableRow = new TableRow(getApplicationContext());
textView=new TextView(getApplicationContext());
textView.setText("Firstname");
textView.setTextColor(Color.RED);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
textView4=new TextView(getApplicationContext());
textView4.setText("LastName");
textView4.setTextColor(Color.RED);
textView4.setTypeface(null, Typeface.BOLD);
textView4.setPadding(20, 20, 20, 20);
tableRow.addView(textView4);
textView5=new TextView(getApplicationContext());
textView5.setText("Email");
textView5.setTextColor(Color.RED);
textView5.setTypeface(null, Typeface.BOLD);
textView5.setPadding(20, 20, 20, 20);
tableRow.addView(textView5);
tableLayout.addView(tableRow);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("fname")));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex("lname")));
textView3 = new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex("email")));
textView1.setPadding(20, 20, 20, 20);
textView2.setPadding(20, 20, 20, 20);
textView3.setPadding(20, 20, 20, 20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
db.close();
}
public void close(View view)
{
System.exit(0);
}
答案 0 :(得分:0)
更新
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TODO, todo.getNote());
values.put(KEY_STATUS, todo.getStatus());
// updating row
return db.update(TABLE_TODO, values, KEY_ID + " = ?",
new String[] { String.valueOf(todo.getId()) });`
删除
public void deleteToDo(long tado_id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_TODO, KEY_ID + " = ?",
new String[] { String.valueOf(tado_id) });
}
答案 1 :(得分:0)
您可以使用
"DELETE * FROM Student WHERE fname LIKE %"+fname_variable+"% OR lname LIKE %"+lname_variable+"% OR email LIKE %"+email_variable+"%;"
用于删除,如果要更新fname
"UPDATE Student SET fname="+new_fname_var+" WHERE fname="+old_fname_var+";"
希望它有所帮助。 :)
答案 2 :(得分:0)
看一下本教程:
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
更新数据库中的记录,请使用此
dbName.update (String table, ContentValues values, String whereClause, String[] whereArgs)