不懂Android Studio如何連接SQLite數(shù)據(jù)庫??其實(shí)想解決這個問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。
創(chuàng)新互聯(lián)建站一直通過網(wǎng)站建設(shè)和網(wǎng)站營銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實(shí)效"的一站式服務(wù),以網(wǎng)站制作、網(wǎng)站設(shè)計、移動互聯(lián)產(chǎn)品、網(wǎng)絡(luò)營銷推廣服務(wù)為核心業(yè)務(wù)。十多年網(wǎng)站制作的經(jīng)驗,使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標(biāo)準(zhǔn)網(wǎng)站,不但價格便宜而且實(shí)用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡單易用,維護(hù)方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。
1、先看一下項目目錄:
2、新建一個AS項目,創(chuàng)建如上圖所示的目錄結(jié)構(gòu),然后添加內(nèi)容:
(1)修改添加布局文件:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
activity_register.xml:
<?xml version="1.0" encoding="utf-8"?>
(2)在service包DatabaseHelper中添加鏈接AS自帶數(shù)據(jù)庫以及創(chuàng)建表的語句:
package com.example.sqlitelogin.service; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { static String name="user.db"; static int dbVersion=1; public DatabaseHelper(Context context) { super(context, name, null, dbVersion); } public void onCreate(SQLiteDatabase db) { String sql="create table user(id integer primary key autoincrement,username varchar(20),password varchar(20),age integer,sex varchar(2))"; db.execSQL(sql); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
(3)在service包UserService中用sql語句寫登錄注冊功能的實(shí)現(xiàn):
package com.example.sqlitelogin.service; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.sqlitelogin.User; public class UserService { private DatabaseHelper dbHelper; public UserService(Context context){ dbHelper=new DatabaseHelper(context); } public boolean login(String username,String password){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="select * from user where username=? and password=?"; Cursor cursor=sdb.rawQuery(sql, new String[]{username,password}); if(cursor.moveToFirst()==true){ cursor.close(); return true; } return false; } public boolean register(User user){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="insert into user(username,password,age,sex) values(?,?,?,?)"; Object obj[]={user.getUsername(),user.getPassword(),user.getAge(),user.getSex()}; sdb.execSQL(sql, obj); return true; } }
(4)在User文件中聲明要用到的表列名的變量,并對其添加get&&set方法:
package com.example.sqlitelogin; import java.io.Serializable; public class User implements Serializable{ private int id; private String username; private String password; private int age; private String sex; public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password, int age, String sex) { super(); this.username = username; this.password = password; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + ", sex=" + sex + "]"; } }
(5)為注冊功能添加activity組件:
package com.example.sqlitelogin; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import com.example.sqlitelogin.service.UserService; public class RegisterActivity extends AppCompatActivity { EditText username; EditText password; EditText age; RadioGroup sex; Button register; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); findViews(); register.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String name=username.getText().toString().trim(); String pass=password.getText().toString().trim(); String agestr=age.getText().toString().trim(); String sexstr=((RadioButton)RegisterActivity.this.findViewById(sex.getCheckedRadioButtonId())).getText().toString(); Log.i("TAG",name+"_"+pass+"_"+agestr+"_"+sexstr); UserService uService=new UserService(RegisterActivity.this); User user=new User(); user.setUsername(name); user.setPassword(pass); user.setAge(Integer.parseInt(agestr)); user.setSex(sexstr); uService.register(user); Toast.makeText(RegisterActivity.this, "注冊成功", Toast.LENGTH_LONG).show(); } }); } private void findViews() { username=(EditText) findViewById(R.id.usernameRegister); password=(EditText) findViewById(R.id.passwordRegister); age=(EditText) findViewById(R.id.ageRegister); sex=(RadioGroup) findViewById(R.id.sexRegister); register=(Button) findViewById(R.id.Register); } }
(6)為登錄功能添加activity組件:
package com.example.sqlitelogin; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.sqlitelogin.service.UserService; public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//即activity_login.xml findViews(); } private EditText username; private EditText password; private Button login; private Button register; private void findViews() { username=(EditText) findViewById(R.id.username); password=(EditText) findViewById(R.id.password); login=(Button) findViewById(R.id.login); register=(Button) findViewById(R.id.register); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=username.getText().toString(); System.out.println(name); String pass=password.getText().toString(); System.out.println(pass); Log.i("TAG",name+"_"+pass); UserService uService=new UserService(LoginActivity.this); boolean flag=uService.login(name, pass); if(flag){ Log.i("TAG","登錄成功"); Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_LONG).show(); Intent intent = new Intent(LoginActivity.this,RegisterActivity.class); startActivity(intent); }else{ Log.i("TAG","登錄失敗"); Toast.makeText(LoginActivity.this, "登錄失敗", Toast.LENGTH_LONG).show(); } } }); register.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent=new Intent(LoginActivity.this,RegisterActivity.class); startActivity(intent); } }); } }
3、Androidmanifest.xml清單文件中,程序運(yùn)行必備的內(nèi)容一般都已經(jīng)自動完成添加了。也可以進(jìn)行修改:
<?xml version="1.0" encoding="utf-8"?>
4、在模擬器或者真機(jī)運(yùn)行程序,即可!一個連接數(shù)據(jù)庫的登錄注冊功能已經(jīng)實(shí)現(xiàn),效果如下:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享Android Studio如何連接SQLite數(shù)據(jù)庫?內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!