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. Asp.Net-创建网站的快捷方式到桌面,开始菜单,收藏夹

            /// <summary>         /// 创建快捷方式         /// </summary>         /// <param na ...

  2. AjaxPro.2使用小结

    这是我最近没事的时候研究的东东,使用AjaxPro.2.dll,从前台调用后台Ajax方法,希望对各位亲有帮助哦.. 1.首先将AjaxPro.2.dll从网上下载下来,打开VS项目,点击项目,右键- ...

  3. C#用反射判断一个类型是否是Nullable同时获取它的根类型(转自网络)

    在我们的应用程序中我们使用类描述我们的业务对象,为我们产生一些报表之类的,那就依赖大量不同的对象,我们创建一个帮助方法来转换我们的业务对象,或是一个List的业务对象到DataTables. 由于数据 ...

  4. 【Linux C中文函数手册】之 内存和字符串函数

    内存和字符串函数 1) bcmp 比较内存内容 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp表头文件 #include<stri ...

  5. 20150320--安全性,Membership类

    安全性--验证与授权,成员资格与角色. 一.成员资格与角色, 第一步:注册数据库,在VS中运行dos命令窗口中,输入:aspnet_regsql,建立数据库表. 如何启动VS的dos命令窗口,在  . ...

  6. (转)对DotNet分布式应用搭建的考虑

    设计前的考虑和准备工作 1 对业务需求的理解重要性远远胜于对技术架构的理解 2 架构包含技术架构和业务架构 3 没有万能和通用的架构,只有符合自身业务需求的架构 4 架构本身的复杂性要截至在架构设计阶 ...

  7. nyoj_t218(Dinner)

    描述 Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he dec ...

  8. 基于Httpfs访问HDFS的C++实现

    Httpfs是hadoop2.x中hdfs项目的内置应用,基于tomcat和jesery,对外提供完备HDFS操作的RESTful接口,无需安装客户端,可方便实现数据交互,如从windows访问存储在 ...

  9. 在线生成ICO图标、站标

    网上一搜有很多,找了两个比较好用的,分别是http://ico.storyren.com/和http://www.ico.la/,前面的那个好像更好点.上传png.jpg.或gif格式的图片,按自己需 ...

  10. C# 语言如何获取json格式的数据,不用javascript用c#实现。。。

    {'state' : 1,'data':{'list':[{'id': 123, 'name': '诸葛天邪','level': 10,'country': 1,}]}} 比如我要获取里面的 id 该 ...