Home > Mobile >  Android the query data display on the page display not to come out, the code with the great spirit s
Android the query data display on the page display not to come out, the code with the great spirit s

Time:10-27

MainActivity code

Package com. Example. Administrator. Databasetest;

The import android. Content. ContentValues;
The import android. Database. Cursor;
The import android. Database. Sqlite. SQLiteDatabase;
The import android. Support. V7. App. AppCompatActivity;
The import android. OS. Bundle;
The import android. Util. Log;
The import android. View. The view;
The import android. Widget. The Button;
The import android. Widget. The EditText;
The import android. Widget. TextView;

Public class MainActivity extends AppCompatActivity {
Private MyDatabaseHelper dbHelper;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main);
DbHelper=new MyDatabaseHelper (this, "BookStore. Db", null, 2);
The Button createDatabase=(Button) the findViewById (R.i, dc reate_database);
CreateDatabase. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
DbHelper. GetWritableDatabase ();
}
});
The Button addData=https://bbs.csdn.net/topics/(Button) the findViewById (da dd_data R.i);
AddData. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
SQLiteDatabase db=dbHelper. GetWritableDatabase ();
ContentValues values=new ContentValues ();
Values. The put (" name ", "The Da Vinci Code");
Values. The put (" author ", "Dan Brown");
Values. The put (" pages ", 454);
Values. The put (" price ", 16.96);
The insert (" Book ", null values);
Values. The clear ();
Values. The put (" name ", "The Lost Symbol");
Values. The put (" author ", "Dan Brown");
Values. The put (" pages ", 510);
Values. The put (" price ", 19.95);
The insert (" Book ", null values);
}
});
The Button updataData=https://bbs.csdn.net/topics/(Button) the findViewById (R.i d.u pdate_data);
UpdataData. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
SQLiteDatabase db=dbHelper. GetWritableDatabase ();
ContentValues values=new ContentValues ();
Values. The put (" price ", 10.5);
The update (" Book ", values, "name=?" The new String [] {} "The Da Vinci Code");
}
});

The Button deleteButton=(Button) the findViewById (R.i d.d elete_data);
DeleteButton. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
SQLiteDatabase db=dbHelper. GetWritableDatabase ();
The delete (" Book ", "pages & gt; ?" , new String [] {" 500 "});
}
});
The Button queryButton=(Button) the findViewById (R.i d.q uery_data);
QueryButton. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
SQLiteDatabase db=dbHelper. GetWritableDatabase ();
Cursor Cursor=db. Query (" Book ", null, null, null, null, null, null);
{if (cursor. MoveToFirst ())
Do {
String name=cursor. Get String (cursor. GetColumnIndex (" name "));
String author=cursor. Get String (cursor. GetColumnIndex (" author "));
Int pages=cursor. Get int (cursor. GetColumnIndex (" pages "));
Double price=cursor. The getDouble (cursor. GetColumnIndex (" price "));


TextView mz=(TextView) the findViewById (R.i d.m z);
Mz. SetText (" title "+ name);
TextView zz=(TextView) the findViewById (R.i d.z z);
Zz. SetText (" author "+ author);
TextView ys=(TextView) the findViewById (R.i d.y s);
Ys. SetText (" pages "+ pages);
Surprised TextView jg=(TextView) the findViewById (R.i jeter g);
Jg. SetText (" price "+ price);

} while (cursor. MoveToNext ());
} cursor. The close ();
}
});

}


}



MyDatabaseHelper code

Package com. Example. Administrator. Databasetest;

The import android. The content. The Context;
The import android. Database. Sqlite. SQLiteDatabase;
The import android. Database. Sqlite. SQLiteOpenHelper;
The import android. Widget. Toast;

/* *
* Created by Administrator on 2019/6/14.
*/

Public class MyDatabaseHelper extends SQLiteOpenHelper {
Public static final String CREATE_BOOK="create table Book ("
+ "id integer primary key autoincrement,"
+ "author text,"
+ price "real,"
+ pages "integer,"
+ "name text)";
Public static final String CREATE_CATEGORY="create table Category ("
+ "id integer primary key autoincrement,"
+ "category_name text,"
"+" category_code integer);
Private Context mContext;
Public MyDatabaseHelper (Context Context, String name, SQLiteDatabase. CursorFactory factory, int version)
{
Super (context, name, factory, version);
MContext=context;
}
@ Override
Public void onCreate (SQLiteDatabase db) {
Db. ExecSQL (CREATE_BOOK);
Db. ExecSQL (CREATE_CATEGORY);
Toast. MakeText (mContext, "create success", Toast. LENGTH_SHORT), show ();
}
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
Db. ExecSQL (drop table if the exists "Book");
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related