android中sql关于时间的操作

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。