您好,登錄后才能下訂單哦!
將用戶的數據進行存儲,利用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;//黑線是查詢數據
}
}
這樣,就可以存儲數據啦!!!!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。