package com.example.myapp;
import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Db mydb;
Button login=null;
Button regin=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb=new Db(this,"myuser.db3",null, 1);
login=(Button)findViewById(R.id.login);
login.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String username=((EditText)findViewById(R.id.username)).getText().toString();
String password=((EditText)findViewById(R.id.password)).getText().toString();
SQLiteDatabase dbRead = mydb.getReadableDatabase();
Cursor c=dbRead.rawQuery("select * from user where username=? and password=?", new String[]{username,password});
if(c.getCount()==0){
Toast.makeText(MainActivity.this, "登陆失败!", 8000).show();
}else{
Toast.makeText(MainActivity.this, "登陆成功!", 8000).show();
}
c.close();
dbRead.close();
}
});
regin=(Button)findViewById(R.id.regin);
regin.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String username=((EditText)findViewById(R.id.username)).getText().toString();
String password=((EditText)findViewById(R.id.password)).getText().toString();
SQLiteDatabase dbWrite = mydb.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", username);
values.put("password", password);
dbWrite.insert("user", null, values);
dbWrite.close();
Toast.makeText(MainActivity.this, "注册成功!", 8000).show();
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.myapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Db extends SQLiteOpenHelper {
public Db(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String mydb="create table user(_id integer primary key autoincrement,username,password)";
db.execSQL(mydb);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}