首页 > 代码库 > sqllite数据库中对时间的操作
sqllite数据库中对时间的操作
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 = http://www.mamicode.com/{ Integer.toString(id) };>2.mainactivity中类简单应用
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
sqllite数据库中对时间的操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。