package com.baclock.entity;

import android.provider.BaseColumns;

/**
* Created by Jack on 5/4/2016.
*/
public final class DBEntity {
// To prevent someone from accidentally instantiating the LocationTrackEntity class,
// give it an empty constructor.
public DBEntity() {}; public static abstract class SyncData implements BaseColumns {
public static final String TABLE_NAME = "syncdata";
public static final String COLUMN_NAME_ACTION_TYPE = "action_type";
public static final String COLUMN_NAME_TOKEN = "token";
public static final String COLUMN_NAME_TOKEN_SECRET = "token_secret";
public static final String COLUMN_NAME_LATITUDE = "latitude";
public static final String COLUMN_NAME_LONGITUDE = "longitude";
public static final String COLUMN_NAME_IP_ADDRESS = "ip";
public static final String COLUMN_NAME_HOST_NAME = "host_name";
public static final String COLUMN_NAME_REASON_START = "reason_start";
public static final String COLUMN_NAME_REASON_END = "reason_end";
public static final String COLUMN_NAME_REASON = "reason";
public static final String COLUMN_NAME_LOCAL_DATE = "local_date";
public static final String COLUMN_NAME_SERVICE = "service";
}
}

  

package com.baclock.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import com.baclock.entity.DBEntity.*; /**
* Created by Jack on 5/4/2016.
*/
public class DBHelper extends SQLiteOpenHelper { // If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "BAClock.db"; private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ","; private static final String SQL_CREATE_SYNC_DATA =
"CREATE TABLE " + SyncData.TABLE_NAME + " (" +
SyncData._ID + " INTEGER PRIMARY KEY," +
SyncData.COLUMN_NAME_ACTION_TYPE + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_TOKEN + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_TOKEN_SECRET + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_LATITUDE + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_LONGITUDE + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_IP_ADDRESS + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_HOST_NAME + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_REASON_START + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_REASON_END + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_REASON + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_LOCAL_DATE + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_SERVICE + TEXT_TYPE +
" )"; private static final String SQL_DELETE_SYNC_DATA =
"DROP TABLE IF EXISTS " + SyncData.TABLE_NAME; public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_SYNC_DATA);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_SYNC_DATA);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}

  

    public static DBHelper dbHelper = null;
public static SQLiteDatabase dbReader = null;
public static SQLiteDatabase dbWriter = null; try {
dbHelper = new DBHelper(getBaseContext());
dbWriter = dbHelper.getWritableDatabase();
dbReader = dbHelper.getReadableDatabase();
} catch (Exception e) {
e.printStackTrace();
} // Read data
try {
String[] scheduled_interval = {
DBEntity.ScheduledInterval._ID,
DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_INTERVAL,
DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_FROM,
DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_TO
}; Cursor cursor = MainActivity.dbReader.query(
DBEntity.ScheduledInterval.TABLE_NAME, // The table to query
scheduled_interval, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
DBEntity.ScheduledInterval._ID // The sort order
); if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
FrequencyEntity entity = new FrequencyEntity();
entity.setScheduledInterval(cursor.getInt(cursor.getColumnIndex(DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_INTERVAL)));
entity.setScheduledFrom(cursor.getString(cursor.getColumnIndex(DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_FROM)));
entity.setScheduledTo(cursor.getString(cursor.getColumnIndex(DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_TO)));
MainActivity.frequencyArrayList.add(entity);
}
}
} catch (Exception e) {
e.printStackTrace();
} // Insert Data
ContentValues params = new ContentValues();
params.put(DBEntity.SyncData.COLUMN_NAME_ACTION_TYPE, "");
params.put(DBEntity.SyncData.COLUMN_NAME_TOKEN, PersistenceUtils.getSharedInstance().get("token", ""));
params.put(DBEntity.SyncData.COLUMN_NAME_TOKEN_SECRET, PersistenceUtils.getSharedInstance().get("token_secret", ""));
if ("".equals(AppUtils.getSharedInstance().getIp()))
params.put(DBEntity.SyncData.COLUMN_NAME_IP_ADDRESS, AppUtils.getSharedInstance().getLocalIP(this));
else
params.put(DBEntity.SyncData.COLUMN_NAME_IP_ADDRESS, AppUtils.getSharedInstance().getIp());
params.put(DBEntity.SyncData.COLUMN_NAME_HOST_NAME, AppUtils.getSharedInstance().getPhoneName()); if (GPSLocationManager.getInstance().getMyLocation() != null) {
params.put(DBEntity.SyncData.COLUMN_NAME_LATITUDE, GPSLocationManager.getInstance().getMyLocation().getLatitude());
params.put(DBEntity.SyncData.COLUMN_NAME_LONGITUDE, GPSLocationManager.getInstance().getMyLocation().getLongitude());
} else {
params.put(DBEntity.SyncData.COLUMN_NAME_LATITUDE, 0);
params.put(DBEntity.SyncData.COLUMN_NAME_LONGITUDE, 0);
}
params.put(DBEntity.SyncData.COLUMN_NAME_REASON_START, "");
params.put(DBEntity.SyncData.COLUMN_NAME_REASON_END, "");
params.put(DBEntity.SyncData.COLUMN_NAME_REASON, ""); SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date curDate0 = new Date();
String date0 = sdf0.format(curDate0); SimpleDateFormat sdf1 = new SimpleDateFormat("hh:mm:ss aa", Locale.US);
String time = sdf1.format(curDate0); SimpleDateFormat sdf2 = new SimpleDateFormat("E, MMM dd", Locale.US);
String date1 = sdf2.format(curDate0); Log.e("date", date1 + " " + time); params.put(DBEntity.SyncData.COLUMN_NAME_LOCAL_DATE, date0);
params.put(DBEntity.SyncData.COLUMN_NAME_SERVICE, "ClockIn");
long newRowId = dbWriter.insert(DBEntity.SyncData.TABLE_NAME, null, params); // Delete Data
if (rowid > 0) {
// Define 'where' part of query.
String selection = DBEntity.ScheduledData._ID + " = ?";
// Specify arguments in placeholder order.
String[] selectionArgs = { String.valueOf(rowid) };
// Issue SQL statement.
MainActivity.dbWriter.delete(DBEntity.ScheduledData.TABLE_NAME, selection, selectionArgs);
}

  

数据库SQLiteDatabase的更多相关文章

  1. 轻量数据库SQLiteDataBase的相关操作方法

    一.查询操作: 查询操作比较复杂,主要有如下操作: db.rawQuery(String sql, String[] selectionArgs); db.query(String table, St ...

  2. Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase

    数据库的创建,获取,执行sql语句: 框架搭建:dao 思考: 1.数据库保存在哪里? 2.如何创建数据库?如何创建表? 3.如何更新数据库?如何更改表的列数据? 4.如何获取数据库? 5.如何修改数 ...

  3. Xamarin.Android 本地数据库 SQLiteDatabase 操作

    目的:使用 SQLiteDatabase 创建本地数据库.表,并对数据进行增删改查操作. 引用命名空间: using Android.App; using Android.Widget; using ...

  4. Android数据存储之SQLCipher数据库加密

    前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...

  5. Android-Sqlite数据库的操作

    Sqlite数据库的简单操作: 设置增删改查的按钮,xml界面布局设置 <?xml version="1.0" encoding="utf-8"?> ...

  6. greendao操作数据库的使用方法

    第一步:把greendao-1.3.0-beta-1,greendao-generator-1.3.1两个jar包加载到工程的lib的文件夹中,一定要右键点击Add As Library后才能使用. ...

  7. SQLite数据库的基本操作

    SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产 ...

  8. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  9. Android SQLite数据库使用

    在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一. ...

随机推荐

  1. HTML5_用语义化标记重新定义博客

    HTML5文档的第一行便是文档类型声明,文档类型声明的作用有两个 一:验证器依据文档类型来判断采用何种验证规则去验证代码 二:文档类型声明能够强制IE6,IE7,IE8以“标准模式”渲染页面 1: & ...

  2. 七、Android学习笔记_JNI hello world

    1.需要准备的工具,eclipse,cdt(c++)插件,cygwin(unix)和 android ndk. 在cygwin的etc目录下将ndk的路径引入到profile文件中,可以在cygwin ...

  3. 第三方框架、AFN、ASI、SDWebImage

    AFN 一.2大管理对象1.AFHTTPRequestOperationManager* 对NSURLConnection的封装 2.AFHTTPSessionManager* 对NSURLSessi ...

  4. UIAlertView[警告框] [代理协议型]UIActionSheet [表单视图][代理协议型]

    ////  ViewController.h//  UIAlertViewAndUIActionSheet////  Created by hehe on 15/9/21.//  Copyright ...

  5. Regsvr32注册ActiveX控件

    命令:Regsvr32 XX.dll 注册ActiveX控件 Regsvr32命令参数:/u 卸载ActiveX控件/s 注册成功后不显示操作成功信息框/c 控制台输出/I 调用DllInstall安 ...

  6. UI4_LabelChess

    // // AppDelegate.m // UI4_LabelChess // // Created by zhangxueming on 15/6/29. // Copyright (c) 201 ...

  7. CentOS6.4下使用默认的PDF文档阅读器出现乱码的解决方案

    方法一:修改/etc/fonts/conf.d/49-sansserif.conf文件,如下: 1: <?xml version="1.0"?> 2: <!DOC ...

  8. Mysql表基本操作

    一. 创建表的方法 语法:create table 表名( 属性名数据类型完整约束条件, 属性名数据类型条完整约束件, ......... 属性名数据类型 ); (1)举例:1 create tabl ...

  9. 如何使用js捕获css3动画

    如何使用js捕获css3动画 css3动画功能强大,但是不像js,没有逐帧控制,但是可以通过js事件来确定任何动画的状态. 下面是一段css3动画代码: #anim.enable{ -webkit-a ...

  10. PHP中$_REQUEST中包含的数据,数据被覆盖问题

    这个问题涉及到php.ini中的两个变量. variables_order = "EGPCS" variables_order 系统在定义PHP预定义变量,EGPCS 是 Envi ...