您好,登錄后才能下訂單哦!
將用戶的數(shù)據(jù)進(jìn)行存儲,利用SQLiteOpenHelper
1.建一個mysqlhelp類,擴(kuò)展自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就是寫進(jìn)去的名字
if("".equals(playername)) //如果EditText里為空,提示選一個名字
{
Toast.makeText(userActivity.this, "選一個名字吧!", Toast.LENGTH_SHORT).show();
}
else{//如果EditText不為空,檢測數(shù)據(jù)庫里是否有這個名字,沒有就插入,有就提示已存在
if(!queryData(myhelper, playername)){
SQLiteDatabase db = myhelper.getWritableDatabase();
//使用insert方法向表中插入數(shù)據(jù)
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();
//畫黑線的是插入數(shù)據(jù)
}
else{
Toast.makeText(userActivity.this, "名字重復(fù)!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
//qurryData方法,查詢數(shù)據(jù)庫里是否存在名字,如果存在就返回true
public static boolean queryData(mysqlhelp myHelper,String user){
//獲得數(shù)據(jù)庫對象
int i=0;
boolean t=false;
SQLiteDatabase db = myHelper.getReadableDatabase();
//查詢表中的數(shù)據(jù)
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();//關(guān)閉結(jié)果集
//db.close();//關(guān)閉數(shù)據(jù)庫對象
return t;//黑線是查詢數(shù)據(jù)
}
}
這樣,就可以存儲數(shù)據(jù)啦?。。。?br />
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。