Operation of time in Sqllite database

Source: Internet
Author: User
Tags add time how to add time

SQL has the type of time, Date,time,datetime, convenient for the maintenance of records, the following demo shows how to add time by default in each record

SOURCE http://www.codes51.com/code/detail_1460.html

1. First the Databasehelper class, where a table is built to hold a book, including the title, author.

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";p rivate final static int database_version = 1;public final static String table_name = "b  Ooks_table ";p ublic final static string book_id =" book_id ";p ublic final static string book_name =" Book_name ";p ublic final static String Book_author = "Book_author";p ublic databasehelper (Context context) {//TODO auto-generated constructor stub Super (context, database_name, NULL, database_version);} Create Table@overridepublic 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_time STAMP); "; Db.execsql (SQL);} @OverridepuBlic 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; Add operation public Long Insert (string bookname, string author) {Sqlitedatabase db = This.getwritabledatabase ();/* Contentvalue s */contentvalues cv = new Contentvalues () cv.put (Book_name, bookname); Cv.put (Book_author, AUTHOR); Long row = Db.insert ( TABLE_NAME, NULL, CV); return row;} Delete operation public void delete (int id) {Sqliedatabase db = This.getwritabledatabase (); String where = book_id + "=?"; String[] Wherevalue = {integer.tostring (id)};d b.delete (table_name, where, wherevalue);} Modify Operation 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);d b.update (TABLE_NAME, CV, where, Wherevalue);} Public Sqlitedatabase Getdb () {Sqlitedatabase db = This.getreadabledatabase (); return db;}}

2.mainactivity class simple application

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 {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);D Atabasehelper Databasehelper = new Databasehelper (this);d atabasehelper.getdb (). Delete (databasehelper.table_name, NULL, NULL); Databasehelper.insert ("A", "AA");d Atabasehelper.insert ("b", "BB");d Atabasehelper.insert ("C", "CC"); Databasehelper.insert ("D", "DD");d Atabasehelper.insert ("E", "ee");d Atabasehelper.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. For more operation and type of time refer to official documentation http://www.sqlite.org/lang_datefunc.html

Operation of time in Sqllite database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.