• 结对项目:一寸时光APP(日程管理)二


    建立数据库

    package com.example.myapplication3.db;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import com.example.myapplication3.model.UserInfo;
    import java.util.ArrayList;
    import java.util.List;

    public class DBManager {
    private DBHelper helper;
    private SQLiteDatabase db;

    public DBManager(Context context) {
    helper=new DBHelper(context);
    db=helper.getWritableDatabase();
    }

    //注册用户
    public void insert(UserInfo info){
    ContentValues values=new ContentValues();
    values.put("name",info.getName());
    values.put("pwd",info.getPwd());
    db.insert("user_info",null,values);
    }

    //查询全部用户名
    public List query(){
    Cursor cursor=db.query("user_info",null,null,null,null,null,null);
    List lists=new ArrayList();
    if (cursor.getCount()!=0){
    cursor.moveToFirst();
    do {
    UserInfo info=new UserInfo(cursor.getString(1),cursor.getString(2));
    lists.add(info);
    }while (cursor.moveToNext());
    return lists;
    }
    return lists;
    }

    //判断数据库中此用户是否已存在
    public int find(String name){
    Cursor cursor=db.query("user_info",null,"name=?",new String[]{name},null,null,null);
    if (cursor.getCount()!=0){
    return 1;
    }
    return 0;
    }

    //查询单个用户
    public UserInfo findUser(String name){
    Cursor cursor=db.query("user_info",null,"name=?",new String[]{name},null,null,null);
    if (cursor.getCount()!=0){
    cursor.moveToFirst();
    UserInfo info=new UserInfo();
    info.setName(cursor.getString(cursor.getColumnIndex("name")));
    info.setPwd(cursor.getString(cursor.getColumnIndex("pwd")));
    return info;
    }
    return null;
    }

    public void closeDB(){
    if (db!=null){
    db.close();
    }
    }

    添加日程界面:本界面主要实现日程系统的填写与添加操作。

    public static Boolean insertLog(Context context, String title, String content, String time) {
    Boolean isSuccess = false;
    DBHelper dbHelper = new DBHelper(context);
    SQLiteDatabase dbWrite = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("title", title);
    values.put("time", time);
    values.put("content", content);
    values.put("state",0);
    if (dbWrite.insert("info", null, values) != -1) {
    isSuccess = true;
    }
    dbWrite.close();
    dbHelper.close();

    return isSuccess;
    

    }

  • 相关阅读:
    14-补充内容:MySQl创建用户和授权
    15-可视化工具Navicat的使用
    11-数据的增删改
    12-单表查询
    09-完整性约束
    10-外键的变种 三种关系
    07-数据类型
    08-数据类型(2)
    Mysql 基本语法
    E. K-periodic Garland
  • 原文地址:https://www.cnblogs.com/feibingyu/p/7006924.html
Copyright © 2020-2023  润新知