SQLite implementation program in Android

Source: Internet
Author: User
Tags sqlite

New project events, the project's main program files are: Events.java, Eventsdata.java, Constants.java; the layout file has item.xml.

The Events.java file is the entry file for the project, which is roughly as follows:

The code is as follows Copy Code

Package org.hxsd.events;

Import static android.provider.basecolumns._id;
Import static Org.hxsd.events.Constants.TABLE_NAME;
Import static Org.hxsd.events.Constants.TIME;
Import static Org.hxsd.events.Constants.TITLE;
Import android.app.ListActivity;
Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.os.Bundle;
Import Android.widget.SimpleCursorAdapter;

public class Events extends Listactivity {
Private Eventsdata events;
Fields for queries
private static string[] from = {_id, time, TITLE};
Sort
private static String order_by = time + "DESC";
Show
private static int[] to = {R.id.rowid, r.id.time, r.id.title};

/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Events = new Eventsdata (this);
try{
Add a record
Addevent ("Hello, android!");
Get record information
Cursor Cursor = GetEvents ();
Display record information
showevents (cursor);
}finally{
Close Database
Events.close ();
}
}

/**
* Add data
* @param string
*/
private void Addevent (string string) {
Sqlitedatabase db = Events.getwritabledatabase ();
Contentvalues values = new Contentvalues ();
Values.put (Time, System.currenttimemillis ());
Values.put (TITLE, String);
Db.insertorthrow (table_name, NULL, values);
}

/**
* Get Records
* @return
*/
Private Cursor getevents () {
Sqlitedatabase db = Events.getreadabledatabase ();
Cursor Cursor = db.query (table_name, from, NULL, NULL, NULL, NULL, order_by);
Startmanagingcursor (cursor);
return cursor;
}

/**
* Show Records
* @param cursor
*/
private void Showevents (Cursor Cursor) {
/*stringbuilder builder = new StringBuilder ("Saved events:n");
while (Cursor.movetonext ()) {
Long id = cursor.getlong (0);
Long time = Cursor.getlong (1);
String title = cursor.getstring (2);
Builder.append (ID). Append (":");
Builder.append (Time). Append (":");
Builder.append (title). Append ("n");
}
TextView TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW01);
Textview.settext (builder);

Simplecursoradapter adapter = new Simplecursoradapter (this, r.layout.item, cursor, from, to);
Setlistadapter (adapter);
}
}
Eventsdata.java documents are as follows:

Package org.hxsd.events;

Import static android.provider.basecolumns._id;
Import static Org.hxsd.events.Constants.TABLE_NAME;
Import static Org.hxsd.events.Constants.TIME;
Import static Org.hxsd.events.Constants.TITLE;
Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;

public class Eventsdata extends sqliteopenhelper{
Database name
private static final String database_name = "EVENTS.DB";
Database version
private static final int database_version = 2;

Constructors
Public Eventsdata (context ctx) {
Super (CTX, database_name, NULL, database_version);
}

@Override
public void OnCreate (Sqlitedatabase db) {
Create data Table Events
Db.execsql ("CREATE TABLE" + table_name + "(" + _id + "integer PRIMARY KEY autoincrement," + Time + "integer," + TITLE + ' TEXT not NULL ');
}

@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
If the version changes, delete the table events and then create a new
Db.execsql ("DROP TABLE IF EXISTS" + table_name);
OnCreate (DB);
}

}

Constants.java documents are as follows:

The code is as follows Copy Code

Package org.hxsd.events;

Import Android.provider.BaseColumns;

Public interface Constants extends basecolumns{
Table name
public static final String table_name = "events";
Field name
Public-static final String time = ' time ';
public static final String title = "title";
}
The Item.xml file is a layout file, located under layout under the Res folder, roughly as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:padding= "10dip"
android:orientation= "Horizontal"
>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/rowid"
android:paddingright= "40dip"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/time"
android:layout_torightof= "@id/rowid"
android:paddingright= "40dip"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/title"
android:layout_torightof= "@id/time"
android:paddingright= "40dip"/>
</RelativeLayout>

Related Article

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.