如何从android java和sqlite中的数据库中检索值? [英] How do I retrieve the value from database in android java and sqlite?
本文介绍了如何从android java和sqlite中的数据库中检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我是sqlite和android的新手。我正在尝试开发一个Android应用程序,我必须登录。我已经建立了一个数据库,但我没有确认它是否已连接并提供正确的查询输出或不是??????请帮帮我......我想在屏幕上看到表格的结果。
hello, I am new to sqlite and android. i m trying to develop an android app in which i have to do login. i had made a database but i am not confirmed that whether it is connected and giving the correct output of query or not?????? please help me..... i want to see the results of tables on the screen.
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
public void login(View view){
String[][] userPass = { { "user1", "pass1" },
{ "user2", "pass2" },
{ "user3", "pass3" } };
String value1 = "userToCheck";
String value2 = "passToCheck";
boolean userOk = false;
for (String[] up : userPass)
if (value1.equals(up[0]) && value2.equals(up[1]))
userOk = true;
System.out.println("User authenticated: " + userOk);
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
}
public void onLocationChanged(Location loc) {
username.setText("");
username.setVisibility(View.INVISIBLE);
Toast.makeText(
getBaseContext(),
"Location changed : Lat: " + loc.getLatitude() + " Lng: "
+ loc.getLongitude(), Toast.LENGTH_SHORT).show();
String longitude = "Longitude: " + loc.getLongitude();
String TAG = null;
Log.v(TAG, longitude);
String latitude = "Latitude: " + loc.getLatitude();
Log.v(TAG, latitude);
/*----------to get City-Name from coordinates ------------- */
String cityName = null;
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
cityName = addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
String s = longitude + "\n" + latitude + "\n\nMy Currrent City is: "
+ cityName;
username.setText(s);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class DBOperations {
// Database fields
private static DataBaseWrapper dbHelper;
private String[] User_TABLE_COLUMNS = { DataBaseWrapper.username,DataBaseWrapper.userid, DataBaseWrapper.password};
private SQLiteDatabase database;
private static DBOperations operations = null;
private DBOperations()
{
}
public DBOperations(Context context) {
dbHelper = new DataBaseWrapper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public static DBOperations getInstance(Context context)
{
if(operations == null)
{
dbHelper = new DataBaseWrapper(context);
operations = new DBOperations();
}
return operations;
}
public UserClass adduser(String number, String string) {
ContentValues values = new ContentValues();
values.put(DataBaseWrapper.username, string);
values.put(DataBaseWrapper.password, number);
long userid = database.insert(DataBaseWrapper.User, null, values);
// now that the user is registered return it ...
Cursor cursor = database.query(DataBaseWrapper.User,
User_TABLE_COLUMNS, DataBaseWrapper.userid + " = "
+ userid, null, null, null, null);
cursor.moveToFirst();
UserClass newuser = parseUser(cursor);
cursor.close();
return newuser;
}
public class DataBaseWrapper extends SQLiteOpenHelper {
//---------------------Table Users-------------------------
public static final String User = "User";
public static final String userid = "userid";
static final String username="username";
static final String password="password";
private static final String DATABASE_NAME = "fyp.db";
protected static final int DATABASE_VERSION = 1;
private static final String UserCreate = "create table " + User
+ "(" + userid + " integer primary key autoincrement, "
+ username + " text not null);";
public DataBaseWrapper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(UserCreate);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// you should do some logging in here
// ..
db.execSQL("DROP TABLE IF EXISTS "+User);
onCreate(db);
}
}
推荐答案
参考:在Android上使用SQLite数据库 [ ^ ]
这篇关于如何从android java和sqlite中的数据库中检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文