将用户的数据进行存储,利用SQLiteOpenHelper
1.建一个mysqlhelp类,扩展自SQLiteOpenHelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class mysqlhelp extends SQLiteOpenHelper {
public mysqlhelp(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
//在这里写sql语句
String sql="create table if not exists userstable("+"id integer primary key,"+"name varchar,"+"grade integer)";//新建一个表
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
2.在Layout中写一个EditText,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/userlogin">
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:inputType="none"
android:hint="请输入你的名字"
android:textColor="#00FFFF" >
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="156dp"
android:text="你的名字:"
android:textSize="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="注册信息" />
</RelativeLayout>
3.在MainActivity中:
import com.example.sql.mysqlhelp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class userActivity extends Activity {
private Button button;
private EditText name;
public static String playername;
mysqlhelp myhelper=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userlogin);
myhelper = new mysqlhelp(this, "myuser.db", null, 1);//*******************************
button=(Button)findViewById(R.id.button1);
name=(EditText)findViewById(R.id.name);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
playername=name.getText().toString();//playername就是写进去的名字
if("".equals(playername)) //如果EditText里为空,提示选一个名字
{
Toast.makeText(userActivity.this, "选一个名字吧!", Toast.LENGTH_SHORT).show();
}
else{//如果EditText不为空,检测数据库里是否有这个名字,没有就插入,有就提示已存在
if(!queryData(myhelper, playername)){
SQLiteDatabase db = myhelper.getWritableDatabase();
//使用insert方法向表中插入数据
ContentValues values = new ContentValues();
values.put("name",playername);
values.put("grade", 0);
db.insert("userstable", "id", values);
Toast.makeText(userActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
values.clear();
db.close();
//画黑线的是插入数据
}
else{
Toast.makeText(userActivity.this, "名字重复!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
//qurryData方法,查询数据库里是否存在名字,如果存在就返回true
public static boolean queryData(mysqlhelp myHelper,String user){
//获得数据库对象
int i=0;
boolean t=false;
SQLiteDatabase db = myHelper.getReadableDatabase();
//查询表中的数据
Cursor cursor = db.query("userstable", null, null, null, null, null, "id asc");
//获取name列的索引
int nameIndex = cursor.getColumnIndex("name");
for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
i++;
if(user.equals(cursor.getString(nameIndex))){
t=true;
return t;
}
}
cursor.close();//关闭结果集
//db.close();//关闭数据库对象
return t;//黑线是查询数据
}
}
这样,就可以存储数据啦!!!!