Basic use of the android orm ing framework (similar to hibernate)

Source: Internet
Author: User

The android orm ing framework allows you to operate databases like hibernate. The following code is extracted from the Internet for your reference only.

 


Package com. cng. utils;
 
Import java. SQL. SQLException;
 
Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. util. Log;
 
Import com. cng. modal. Hello;
 
Import com. j256.ormlite. android. apptools. OrmLiteSqliteOpenHelper;
Import com. j256.ormlite. dao. Dao;
Import com. j256.ormlite. support. ConnectionSource;
Import com. j256.ormlite. table. TableUtils;
 
Public class DataHelper extends OrmLiteSqliteOpenHelper
{
 
Private static final String DATABASE_NAME = "HelloOrmlite. db ";
Private static final int DATABASE_VERSION = 1;
Private Dao <Hello, Integer> helloDao = null;
 
Public DataHelper (Context context)
{
Super (context, DATABASE_NAME, null, DATABASE_VERSION );
}
 
@ Override
Public void onCreate (SQLiteDatabase db, ConnectionSource connectionSource)
{
Try
{
TableUtils. createTable (connectionSource, Hello. class );
} Catch (SQLException e)
{
Log. e (DataHelper. class. getName (), "failed to create database", e );
E. printStackTrace ();
}
}
 
@ Override
Public void onUpgrade (SQLiteDatabase db, ConnectionSource connectionSource,
Int arg2, int arg3)
{
Try
{
TableUtils. dropTable (connectionSource, Hello. class, true );
OnCreate (db, connectionSource );
} Catch (SQLException e)
{
Log. e (DataHelper. class. getName (), "failed to update database", e );
E. printStackTrace ();
}
}
 
@ Override
Public void close ()
{
Super. close ();
HelloDao = null;
}
 
Public Dao <Hello, Integer> getHelloDataDao () throws SQLException
{
If (helloDao = null)
{
HelloDao = getDao (Hello. class );
}
Return helloDao;
}
}

 

Package com. cng;
 
Import java. SQL. SQLException;
Import java. util. List;
 
Import com. cng. modal. Hello;
Import com. cng. utils. DataHelper;
Import com. j256.ormlite. android. apptools. OrmLiteBaseActivity;
Import com. j256.ormlite. dao. Dao;
 
Import android. OS. Bundle;
Import android. widget. TextView;
 
 
 
 
Public class OrmliteLoginActivity extends OrmLiteBaseActivity <DataHelper>
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView TV = (TextView) this. findViewById (R. id. output );
Try
{
Dao <Hello, Integer> helloDao = getHelper (). getHelloDataDao ();
// Add data
For (int I = 0; I <2; I ++)
{
Hello hello = new Hello ("Hello" + I );
HelloDao. create (hello );
}
TV. setText (TV. getText () + "\ n" + "added data ");
// Query the added data
List <Hello> hellos = helloDao. queryForAll ();
For (Hello h: hellos)
{
TV. setText (TV. getText () + "\ n" + h. toString ());
}
// Delete the first data entry
HelloDao. delete (hellos. get (0 ));
TV. setText (TV. getText () + "\ n" + "data deletion completed ");
// Re-query data
Hellos = helloDao. queryForAll ();
For (Hello h: hellos)
{
TV. setText (TV. getText () + "\ n" + h. toString ());
}
// Modify data
Hello h1 = hellos. get (0 );
H1.setWord ("this is modified data ");
TV. setText (TV. getText () + "\ n" + "data modified ");
HelloDao. update (h1 );
// Re-query data
Hellos = helloDao. queryForAll ();
For (Hello h: hellos)
{
TV. setText (TV. getText () + "\ n" + h. toString ());
}
 
} Catch (SQLException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
}
 
}
}
Package com. cng. modal;
 
Import android. R. integer;
 
Import com. j256.ormlite. field. DatabaseField;
 
Public class Hello
{
@ DatabaseField (generatedId = true, unique = true)
Int id;
@ DatabaseField
String word;
// This must be added; otherwise, an error will occur.
Public Hello (){}
Public int getId ()
{
Return id;
}
Public Hello (String word)
{
Super ();
This. word = word;
}
Public void setId (int id)
{
This. id = id;
}
 
Public String getWord ()
{
Return word;
}
 
Public void setWord (String word)
{
This. word = word;
}
 
@ Override
Public String toString ()
{
StringBuilder sb = new StringBuilder ();
Sb. append ("id ="). append (id );
Sb. append (", word ="). append (word );
Return sb. toString ();
}
Www.2cto.com
}

 

 

For these three classes, datahelper is the class used to operate databases. You can create and update Tables. Hello is a class mapped to database tables, see the specific api documentation is the http://ormlite.com/releases/ (where the jar package is also downloaded here) OrmliteLoginActivity is the activity class, it does not inherit activity, but inherits OrmLiteBaseActivity class.

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.