环境;Android studio3.4+
sqlite数据库基本的增删该查
在这里插入图片描述
package com.admin.sms;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @desc 登录界面
* @author 小强程序设计
* @QQ 2103752102
*/
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_username;
private EditText et_password;
private Button btn_login;
private Button btn_register;
// 数据库的名字
public static String dbName = "before.db";
// 数据库在手机里的路径
@SuppressLint("SdCardPath")
private static String DATABASE_PATH = "/data/data/com.admin.sms/databases/";
public boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String databaseFilename = DATABASE_PATH + dbName;
checkDB = SQLiteDatabase.openDatabase(databaseFilename, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
initData();
// initDB();
}
private void initDB() {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {// 不存在就把raw里的数据库写入手机
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* 复制数据库到手机指定文件夹下 * @throwsIOException
*/
public void copyDataBase() throws IOException {
String databaseFilenames = DATABASE_PATH + dbName;
File dir = new File(DATABASE_PATH);
if (!dir.exists())// 判断文件夹是否存在,不存在就新建一个
dir.mkdir();
FileOutputStream os = null;
try {
os = new FileOutputStream(databaseFilenames);// 得到数据库文件的写入流
} catch (FileNotFoundException e) {
e.printStackTrace();
}
InputStream is = getResources() .openRawResource(R.raw.before);
byte[] buffer = new byte[8192];
int count = 0;
try {
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
os.flush();
}
} catch (IOException e) {
}
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void initData() {
SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
String username = sp.getString("username", "");
String password = sp.getString("password", "");
et_username.setText(username);
et_password.setText(password);
}
private void initView() {
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
btn_login = (Button) findViewById(R.id.btn_login);
btn_register = (Button) findViewById(R.id.btn_register);
btn_login.setOnClickListener(this);
btn_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
submit();
break;
case R.id.btn_register:
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
break;
}
}
private void submit() {
String username = et_username.getText().toString().trim();
if (TextUtils.isEmpty(username)) {
Toast.makeText(this, "账号不能为空", Toast.LENGTH_SHORT).show();
return;
}
String password = et_password.getText().toString().trim();
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
User user = SqliteHelper.login(new SqliteHelper(this), username, password);
if (user != null) {
SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putInt("id", user.getId());
edit.putString("username", username);
edit.putString("password", password);
edit.putString("name", user.getName());
edit.putString("phone", user.getPhone());
edit.putInt("sex", user.getSex());
edit.commit();
startActivity(new Intent(this, MainActivity.class));
finish();
} else {
Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();
}
}
}
有需要完整代码的请加Q : 2103752102