I want to print the whole database data so that I can check if my SQLite code is working or not.
I am fetching the user login details from login.java and want to show that at loggedin.java
我想打印整个数据库数据,以便检查我的SQLite代码是否正常工作。我从login.java获取用户登录详细信息,并希望在loggedin.java中显示该信息
login.java
public class login extends AppCompatActivity {
EditText e1, e2;
ImageView i1, i2;
Retrofit retrofit;
TextView t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText);
if( e1.getText().toString().length() == 0 ){
e1.setError( "email is required" );}
e2 = (EditText) findViewById(R.id.editText2);
if( e2.getText().toString().length() == 0 ){
e2.setError( "password is required" );}
i1 = (ImageView) findViewById(R.id.email);
i2 = (ImageView) findViewById(R.id.password);
Button b1= (Button) findViewById(R.id.button);
t=(TextView)findViewById(R.id.textView5);
retrofit = new Retrofit.Builder()
.baseUrl("http://funnytadka.com:8060")
.addConverterFactory(GsonConverterFactory.create())
.build();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
db.insertEntry(e1.getText().toString(),e2.getText().toString());
loginApi thisapi = retrofit.create(loginApi.class);
Call<Pvideos> call = thisapi.loadData(e1.getText().toString(), e2.getText().toString());
call.enqueue(new Callback<Pvideos>() {
@Override
public void onResponse(Response<Pvideos> response, Retrofit retrofit) {
if (response.isSuccess())
{
Intent intent = new Intent(login.this, loggedin.class);
startActivity(intent);
}
else
{
Toast.makeText(getApplicationContext(),"login unsuccessful",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(getApplicationContext(),"login unsuccessful",Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
loggedin.java
public class loggedin extends AppCompatActivity
{
View view;
// public static final String DEFAULT = "N/A";
TextView t1, t2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accinfo);
t1 = (TextView) findViewById(R.id.textView3);
t2 = (TextView) findViewById(R.id.textView4);
}
databasehandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DICTIONARY_TABLE_NAME = "logindb";
static SQLiteDatabase db;
private static final String DICTIONARY_TABLE_CREATE =
"CREATE TABLE " + DICTIONARY_TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,email text,password text);";
DatabaseHandler(Context context) {
super(context, "logindb", null, DATABASE_VERSION);
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DICTIONARY_TABLE_CREATE);
db = this.db;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void closeDb() {
db.close();
;
}
public static boolean insertEntry(String email, String password) {
ContentValues cv = new ContentValues();
cv.put("email", email);
cv.put("password", password);
Log.e("email", email);
Log.e("pass", password);
long rowsEffected = db.insert(DICTIONARY_TABLE_NAME, null, cv);
return rowsEffected > 0 ? true : false;
}}
1 个解决方案
#1
0
Write a Method to retrieve data from DB table in
编写一个方法从中检索数据库表中的数据
databasehandler.java
public Cursor getUserDetails( ) throws SQLException {
logger.debug("MYDATABASE_TABLE :- "+MYDATABASE_TABLE );
String q = "SELECT * FROM "+MYDATABASE_TABLE+" ;";
logger.debug("query :- "+q);
Cursor mCursor = db.rawQuery(q, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
db.close();
return mCursor;
}
public DBTariffHandler openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
MYDATABASE_VERSION);
db = sqLiteHelper.getReadableDatabase();
return this;
}
loggedin.java
databasehandler handler = new databasehandler(context);
handler.openToRead();
Cursor cursor_next = handler.getUserDetails();
handler.close() ;
if(cursor_next != null && cursor_next.moveToFirst())
{
cursor_next.getColumnIndex(databasehandler.KEY_USERNAME)
//..ETC
}
#1
0
Write a Method to retrieve data from DB table in
编写一个方法从中检索数据库表中的数据
databasehandler.java
public Cursor getUserDetails( ) throws SQLException {
logger.debug("MYDATABASE_TABLE :- "+MYDATABASE_TABLE );
String q = "SELECT * FROM "+MYDATABASE_TABLE+" ;";
logger.debug("query :- "+q);
Cursor mCursor = db.rawQuery(q, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
db.close();
return mCursor;
}
public DBTariffHandler openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
MYDATABASE_VERSION);
db = sqLiteHelper.getReadableDatabase();
return this;
}
loggedin.java
databasehandler handler = new databasehandler(context);
handler.openToRead();
Cursor cursor_next = handler.getUserDetails();
handler.close() ;
if(cursor_next != null && cursor_next.moveToFirst())
{
cursor_next.getColumnIndex(databasehandler.KEY_USERNAME)
//..ETC
}