java ToDo App数据库代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java ToDo App数据库代码相关的知识,希望对你有一定的参考价值。

package com.example.bboyryu.todo;


import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.SQLException;
import android.util.Log;

public class ToDoModel {

    DbOpenHelper helper = null;
    SQLiteDatabase db = null;

    private final static String DATABASE_NAME = "todo";
    private final static String TODO_TABLE_NAME = "mst_todo";

    /*
     * Constract
     * */


    public ToDoModel(Context context){
        if(helper == null || db == null) {
            helper = new DbOpenHelper(context);
            db = helper.getWritableDatabase();
        }
    }

    /*
    private String getTodo(){
        String sql = "SELECT * FROM " + TODO_TABLE_NAME;
        try {
            db.execSQL(sql);

        } catch (SQLException e) {
            Log.e("ERROR", e.toString());
        }
    }
    */
    /*
     * Insert data
     *
     * Parameter
     * @ String title
     * */
    public void insertToDoData(String title){
        String sql = "INSERT INTO " + TODO_TABLE_NAME + " (title, created_at, update_at, delflg) values(?,CURTIME(),CURTIME(),0);";
        String[] bindStr = new String[]{
                title
        };
        try {
            db.execSQL(sql, bindStr);
        } catch (SQLException e) {
            Log.e("ERROR", e.toString());
        }
    }
    /*
     * Update Task
     *
     * Parameter
     * @ int id
     * @ String title
     * @ int delflg
     * */
    /*
    public void updateToDoData(String id ,String title){
        // Update
        String sql = "UPDATE " + TODO_TABLE_NAME
                + " SET title =?, update_at = CURTIME() "
                + "WHERE id = ? ;";
        String[] bindStr = new String[]{
                title,
                id,
        };
        try {
            db.execSQL(sql,bindStr);
        } catch (SQLException e) {
            Log.e("ERROR", e.toString());
        }
    }
    */
    /*
     * Logically delete
     *
     * Parameter
     * @ int id
     * @ String title
     * @ int delflg
     * */
    /*
    public void updateToDoStatus(String id , String delflg){
        // Logically delete
        String sql = "UPDATE " + TODO_TABLE_NAME
                + " SET update_at = CURTIME(),delflg = ?, "
                + "WHERE id = ? ;";
        String[] bindStr = new String[]{
                delflg,
                id,
        };
        try {
            db.execSQL(sql,bindStr);
        } catch (SQLException e) {
            Log.e("ERROR", e.toString());
        }
    }
    */
    /*
     * Logically delete
     *
     * Parameter
     * @int id
     * */
    /*
    public void deleteToDoData(String id){
        String sql = "DELETE FROM " + TODO_TABLE_NAME
                + " WHERE id = ?;";
        String[] bindStr = new String[]{
                id
        };
        try {
            db.execSQL(sql,bindStr);
        } catch (SQLException e) {
            //Log.e("ERROR", e.toString());
        }
    }
    */
    /*
     * Close to database
     *
     */
    /*
    public void closeDataBase() {
        db.close();
        db = null;
    }
    */
    /*
     * Input validation
     * */
    public boolean validateData(String title){
        if(title == null && title.isEmpty()) {
            return false;
        }
        return true;
    }
}
package com.example.bboyryu.todo;

import android.support.v7.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //setContentView(R.layout.sub);

        //データベースヘルパーのインスタンスを作成する(まだデータベースはできない)
        DbOpenHelper dbHelper = new DbOpenHelper(this);
        //データベースオブジェクトを取得する(データベースにアクセスすると作成される。)
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        //ToDoModel model = new ToDoModel(this);
        //model.insertToDoData( "aaa");
        //データベースを閉じる
        //db.close();

    }
}
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbOpenHelper extends SQLiteOpenHelper {


    private static final String TAG = "DbOpenHelper";
    private static final String DATABASE_NAME = "todo";
    private static final String TODO_TABLE_NAME = "mst_todo";
    private static final int DATABASE_VERSION = 1;

    public DbOpenHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        createToDoTable(db);
    }
    /*
     * Update ToDo Table
     * This method execute when application was upgraded
     * Change the database configuration
     * */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS " + TODO_TABLE_NAME + ";");
        onCreate(db);
    }
    /*
     * Initialize process create ToDo Table
     * */
    private void createToDoTable(SQLiteDatabase db){
        String sql = "CREATE TABLE mst_todo ( "
                + " id int(11) NOT NULL COMMENT'Tasku ID' AUTO_INCREMENT, "
                + "Title text NOT NULL COMMENT'Title', "
                + "created_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT'Create Time', "
                + "update_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT'Update Time', "
                + "delflg tinyint(3) NOT NULL DEFAULT 0 COMMENT'Delete Flag', "
                + "PRIMARY KEY (id); ";
        db.execSQL(sql);
    }
}
package com.example.bboyryu.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper {



    /* データベース名 */
    private final static String DB_NAME = "todo.db";
    /* データベースのバージョン */
    private final static int DB_VER = 1;

/*
    private final static String TAG = "DbOpenHelper";
    private final static String DATABASE_NAME = "todo";
    private final static String TODO_TABLE_NAME = "mst_todo";
    private final static int DATABASE_VERSION = 1;
*/

    /*
     * コンストラクタ
     */
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /*
     * Initialize process create ToDo Table
     * */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "";
            sql += "create table MyTable (";
            sql += " id integer primary key autoincrement";
            sql += ",title text not null";
            sql += ",created_at numeric";
            sql += ",update_at numeric";
            sql += ",delflg integer";
            sql += ")";
        db.execSQL(sql);
    }
    /*
     * Update ToDo Table
     * This method execute when application was upgraded
     * Change the database configuration
     * */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS " + TODO_TABLE_NAME + ";");
        onCreate(db);
    }

}

以上是关于java ToDo App数据库代码的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 ant 检查 Java 源代码中的标签(TODO:等)

Java注释中TODO/FIXME/XXX的含义

Laravel 5.6 db:seed 抛出 FatalThrowableError:找不到类 'APP\Todo'

Scala进阶之App特质

Google官方MVP模式示例项目解析 todo-mvp

TODO:搭建Laravel VueJS SemanticUI