MainActivity.java:
public class MainActivity extends ActionBarActivity {
private static final String SAMPLE_DB_NAME = "JBook";
private static final String SAMPLE_TABLE_NAME = "Info";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (LastName VARCHAR, FirstName VARCHAR," +
" Rank VARCHAR);");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('Kirk','James, T','Captain');");
sampleDB.close();
Toast.makeText(this, "DB Created!", Toast.LENGTH_LONG).show();}
这个程序没有错误。 我想在点击按钮的同时通过textview(“krick,james,Captain”)显示值。使用简单的程序并进行注释.Plese提供简单的代码。
答案 0 :(得分:0)
阅读本文: How to retrieve the data from my SQLite database and display them to a textView
希望这会对你有所帮助。
SQLiteDatabase sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
Cursor returnCursor = sampleDB.rawQuery("select * from " + SAMPLE_TABLE_NAME, null);
if(returnCursor != null && returnCursor.moveToFirst()){
String txt1 = returnCursor.getString(0);
String txt2 = returnCursor.getString(1);
String txt3 = returnCursor.getString(2);
returnCursor.close();
}
我没有尝试过代码,可能会出现语法错误。