Control the SQLite database by SQL statements in Eclipse and delete and change

Source: Internet
Author: User

Package Com.xh.tx.dao;

Import java.util.ArrayList;
Import java.util.List;

Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;

Import Com.xh.tx.bean.Person;
Import Com.xh.tx.utils.MySQLLiteHelper;

Public class Persondao
{
Private Mysqllitehelper Helper;
Public Persondao (context context)
{
helper = new Mysqlitehelper (context, NULL, NULL,-1);
}

/** 
* Add operations to the database
* Sql:insert into person (name,age) VALUES (' sz ', +);
* Defects:
transmission parameters are cumbersome.
cannot have a return value
* /

Public void Saveperson (person p)
{
String sql = "INSERT into person (name,age) values (?,?);";
Sqlitedatabase db = Helper.getwritabledatabase ();
//db represents a connection to a database   
if (Db.isopen ())//Determine if the database is open
{
db.execsql (SQL, New Object[]{p.getname (), P.getage ()}); //To add a row of records to the database
Db.close (); //Remember to close the database connection
}
}

/** 
* Delete an object
* Sql:delete from person where _id = 1;
* /
Public void Deleteperson (Integer id)
{
String sql = "Delete from the person where _id =?;";
Sqlitedatabase db = Helper.getwritabledatabase ();
if (Db.isopen ())
{
db.execsql (SQL, new Integer[]{id});
db.close ();
}
}

/** 
* Modify an object
* sql:update person Set name = ' CCCC ' where _id=1;
* /
Public void Updateperson (person p)
{
String sql = "Update person set name =?" where _id=?; ";
Sqlitedatabase db = Helper.getwritabledatabase ();
if (Db.isopen ())
{
db.execsql (SQL, New Object[]{p.getname (), p.get_id ()});
db.close ();
}
}

/** 
* Check All
* /
Public list<person> Queryperson ()
{
String sql = "Select _id,name,age from the person;";
Sqlitedatabase db = Helper.getreadabledatabase ();
list<person> List = null;

if (Db.isopen ())
{
cursor cursor = db.rawquery (sql, NULL); //Enquiry
//cursor.getcount () View the number of result sets returned after executing SQL      
if (null! = Cursor && cursor.getcount () > 0)
{
list = new arraylist<person> ();
While (Cursor.movetonext ())
{
Integer id = cursor.getint (0);
String name = cursor.getstring (1);
Integer age = Cursor.getint (2);
List.add (new Person (id,name,age));
}
}
}
return list;
}
}

Control the SQLite database by SQL statements in Eclipse and delete and change

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.