主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/ed_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入网址的ID"/>
<EditText
android:id="@+id/ed_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入网址名"/>
<EditText
android:id="@+id/ed_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入时间"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加" />
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除" />
<Button
android:id="@+id/update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改" />
<Button
android:id="@+id/query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询" />
</LinearLayout>
创建Bean
创建Dao层
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class Dao {
public static final String TABLE_NAME ="config";
public static final String BEAN_ID ="id";
public static final String BEAN_TITLE ="title";
public static final String BEAN_DATE ="data";
private final SQLiteDatabase database;
public Dao(Context context){
MyHelper helper = new MyHelper(context);
database = helper.getWritableDatabase();
}
//添加
public long add(Bean bean){
ContentValues values = new ContentValues();
values.put(BEAN_ID,bean.getId());
values.put(BEAN_TITLE,bean.getTitle());
values.put(BEAN_DATE,bean.getData());
long add = database.insert(TABLE_NAME, null, values);
return add;
}
//删除
public int del(int id){
int delete = database.delete(TABLE_NAME, BEAN_ID + "=?", new String[]{id + ""});
return delete;
}
//修改
public int update(Bean bean){
ContentValues values = new ContentValues();
values.put(BEAN_ID,bean.getId());
values.put(BEAN_TITLE,bean.getTitle());
values.put(BEAN_DATE,bean.getData());
int update = database.update(TABLE_NAME, values, BEAN_ID + "=?", new String[]{bean.getId() + ""});
return update;
}
//查询
public List<Bean> queryAll(){
Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null, null);
List<Bean> list = new ArrayList<Bean>();
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex(BEAN_ID));
String title = cursor.getString(cursor.getColumnIndex(BEAN_TITLE));
String data = cursor.getString(cursor.getColumnIndex(BEAN_DATE));
Bean bean = new Bean(id, title, data);
list.add(bean);
}
return list;
}
}
创建Myhelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MyHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "web";
public MyHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table "+Dao.TABLE_NAME+"("+Dao.BEAN_ID+" integer primary key autoincrement,"+Dao.BEAN_TITLE+" varchar(20),"+Dao.BEAN_DATE+" varchar(20))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
主页面
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Dao dao;
private EditText id;
private EditText title;
private EditText data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dao = new Dao(MainActivity.this);
id = findViewById(R.id.ed_id);
title = findViewById(R.id.ed_title);
data = findViewById(R.id.ed_data);
findViewById(R.id.add).setOnClickListener(this);
findViewById(R.id.delete).setOnClickListener(this);
findViewById(R.id.update).setOnClickListener(this);
findViewById(R.id.query).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add:
String add_id = id.getText().toString().trim();
int id1 = Integer.parseInt(add_id);
String add_title = title.getText().toString().trim();
String add_data = data.getText().toString().trim();
Bean bean = new Bean(id1,add_title,add_data);
dao.add(bean);
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
String delete_id = id.getText().toString().trim();
int id2 = Integer.parseInt(delete_id);
dao.del(id2);
Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
break;
case R.id.update:
String update_id = id.getText().toString().trim();
int id3 = Integer.parseInt(update_id);
String update_title = title.getText().toString().trim();
String update_data = data.getText().toString().trim();
Bean bean2 = new Bean(id3, update_title, update_data);
dao.update(bean2);
Toast.makeText(MainActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
break;
case R.id.query:
List<Bean> queryAll = dao.queryAll();
Toast.makeText(MainActivity.this,queryAll+"",Toast.LENGTH_SHORT).show();
break;
}
}
}
如果要连接数据库需要新增一个方法
private void add() {
for (int i = 0; i < list.size(); i++) {
String data = list.get(i).getLink();
String title = list.get(i).getTitle();
Bean bean = new Bean(i, data, title);
dao.add(bean);
}
}