码迷,mamicode.com
首页 > 移动开发 > 详细

android中sql关于时间的操作

时间:2015-01-07 08:09:40      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

sql 中有时间的类型,date,time,datetime,方便关于记录的维护,下面一个demo演示怎么在每条记录中默认增加时间

源码下载地址 http://www.codes51.com/code/detail_1460.html

1.先是databasehelper类,这里建表保存一本书,包括书名,作者。

 

package com.chaoyue.sqldemo;

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

public class DatabaseHelper extends SQLiteOpenHelper {
	private final static String DATABASE_NAME = "BOOKS.db";
	private final static int DATABASE_VERSION = 1;
	public final static String TABLE_NAME = "books_table";
	public final static String BOOK_ID = "book_id";
	public final static String BOOK_NAME = "book_name";
	public final static String BOOK_AUTHOR = "book_author";

	public DatabaseHelper(Context context) {
		// TODO Auto-generated constructor stub
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// 创建table
	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + TABLE_NAME + " (" + BOOK_ID
				+ " INTEGER primary key autoincrement, " + BOOK_NAME
				+ " text, " + BOOK_AUTHOR + " text, oderdate DATETIME DEFAULT CURRENT_TIMESTAMP  );";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
		db.execSQL(sql);
		onCreate(db);
	}

	public Cursor select() {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db
				.query(TABLE_NAME, null, null, null, null, null, null);
		return cursor;
	}

	// 增加操作
	public long insert(String bookname, String author) {
		SQLiteDatabase db = this.getWritableDatabase();
		/* ContentValues */
		ContentValues cv = new ContentValues();
		cv.put(BOOK_NAME, bookname);
		cv.put(BOOK_AUTHOR, author);
		long row = db.insert(TABLE_NAME, null, cv);
		return row;
	}

	// 删除操作
	public void delete(int id) {
		SQLieDatabase db = this.getWritableDatabase();
		String where = BOOK_ID + " = ?";
		String[] whereValue = { Integer.toString(id) };
		db.delete(TABLE_NAME, where, whereValue);
		
	}

	// 修改操作
	public void update(int id, String bookname, String author) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = BOOK_ID + " = ?";
		String[] whereValue = { Integer.toString(id) };

		ContentValues cv = new ContentValues();
		cv.put(BOOK_NAME, bookname);
		cv.put(BOOK_AUTHOR, author);
		db.update(TABLE_NAME, cv, where, whereValue);
	}
	
	public SQLiteDatabase getDB(){
		SQLiteDatabase db = this.getReadableDatabase();
		return db;
	}

}

2.mainactivity中类简单应用

 

IT经典笑语录:系统程序员:1、头皮经常发麻,在看见一个蓝色屏幕的时候比较明显,在屏幕上什幺都看不见的时候尤其明显;2、乘电梯的时候总担心死机,并且在墙上找reset键; 3、指甲特别长,因为按F7到F12比较省力; 4、只要手里有东西,就不停地按,以为是Alt-F、S;5、机箱从来不上盖子,以便判断硬盘是否在转; 6、经常莫名其妙地跟踪别人,手里不停按F10;7、所有的接口都插上了硬盘,因此觉得26个字母不够; 8、一有空就念叨“下辈子不做程序员了”; 9、总是觉得9号以后是a号; 10、不怕病毒,但是很害怕自己的程序;

 

package com.chaoyue.sqldemo;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		DatabaseHelper databaseHelper = new DatabaseHelper(this);
		databaseHelper.getDB().delete(DatabaseHelper.TABLE_NAME, null, null);
		databaseHelper.insert("a", "aa");
		databaseHelper.insert("b", "bb");
		databaseHelper.insert("c", "cc");
		databaseHelper.insert("d", "dd");
		databaseHelper.insert("e", "ee");
		databaseHelper.insert("f", "ff");
		Cursor cursor = databaseHelper.select();

		while (cursor.moveToNext()) {
			String name = cursor.getString(cursor.getColumnIndex("book_name"));
			String author = cursor.getString(cursor
					.getColumnIndex("book_author"));
			String oderdate = cursor.getString(cursor
					.getColumnIndex("oderdate"));
			Log.d("", "chaoyue name=" + name + " author=" + author
					+ " oderdate="+oderdate  );
		}
		cursor.close();
	}

}

 3.关于时间的更多操作与类型可以参考官方文档http://www.sqlite.org/lang_datefunc.html

android中sql关于时间的操作

标签:

原文地址:http://www.cnblogs.com/superken/p/4207496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!