Invoking the SQLite database in Android

Source: Internet
Author: User
Tags sqlite database

1 in the Android Studio project, right-click the new Assets folder in the Gradle folder. Import the built SQLite database into it.

2 in the main activity to determine whether the app is the first time to start the call the following method:

/** Check if the app is first launched */
private int checkfirstactivate () {
/* Set Database transfer status */
int setsqlitedatabase_state = 0;
/* Check to see if the app is the first start & transfer database */
Sharedpreferences sharedpreferences = getsharedpreferences ("Activate_state", context.mode_private);
Boolean user_first_activate_state = Sharedpreferences.getboolean ("First", true);
if (user_first_activate_state) {
Sharedpreferences.edit (). Putboolean ("First", false). Apply ();
Appdatabase appdatabase = new Appdatabase ();
Setsqlitedatabase_state = Appdatabase.setsqlitedatabase (Getapplicationcontext ());
return setsqlitedatabase_state;
}else{
return setsqlitedatabase_state;
}
}

----------------------

public class Appdatabase {
public int Setsqlitedatabase (context context) {

String Db_path = "/data/data/com.example.administrator.search/databases/";
String db_name = "testdb.db";
try{
/* Check if the path exists and does not exist to form the path */
File File = new file (Db_path);
if (!file.exists ()) {
File.mkdir ();
}

/* Use the DB file in the Asset folder as the input stream */
InputStream InputStream = Context.getassets (). open (db_name);

/* Database placement path under the build program's root directory */
String outfilename = Db_path + db_name;

/* Generate output stream */
OutputStream outputstream = new FileOutputStream (outfilename);

/* Set byte and convert */
byte[] buffer = new byte[8192];
int lenhth;
while ((lenhth = inputstream.read (buffer)) >0) {
Outputstream.write (buffer,0,lenhth);
}

/* Close IO Stream */
Outputstream.flush ();
Outputstream.close ();
Inputstream.close ();
}catch (IOException e) {
E.printstacktrace ();
return 0;
}
return 1;
}

}
----------------
3 Write a custom tool class to call the database to use SQL statements to find the data to get
public class Gettreeobject {
Public Gettreeobject () {
}
The static class is used to invoke the database and return the Treekepu Table object.
public static Treekepu Treekepu (String s) {
String sql_statement = s;
String Db_path = "/data/data/com.example.administrator.search/databases/";
String db_name = "testdb.db";
String Db_url = Db_path + db_name;
Sqlitedatabase sqlitedatabase = sqlitedatabase.openorcreatedatabase (Db_url, NULL);


cursor cursor = sqlitedatabase.rawquery (sql_statement, NULL);
Treekepu Treekepu = new Treekepu ();
if (Cursor.movetofirst ()) {

try {
Treekepu.settid (Cursor.getint (Cursor.getcolumnindex ("Tid"));
Treekepu.settstyle (Cursor.getstring (Cursor.getcolumnindex ("Tstyle"));
Treekepu.setttype (Cursor.getstring (Cursor.getcolumnindex ("Ttype"));
Treekepu.settname (Cursor.getstring (Cursor.getcolumnindex ("Tname"));
Treekepu.settmoral (Cursor.getstring (Cursor.getcolumnindex ("tmoral"));
Treekepu.setthabit (Cursor.getstring (Cursor.getcolumnindex ("Thabit"));
Treekepu.settlooktype (Cursor.getstring (Cursor.getcolumnindex ("Tlooktype"));
Treekepu.settdiscribe (Cursor.getstring (Cursor.getcolumnindex ("Tdiscribe"));
Treekepu.settalias (Cursor.getstring (Cursor.getcolumnindex ("Talias"));
} catch (Exception e) {
E.printstacktrace ();
}
}
Cursor.close ();
Sqlitedatabase.close ();

return TREEKEPU;
}
------
public class Treekepu {

public int Gettid () {
return Tid;
}
Public String Getttype () {return ttype;}

Public String Gettname () {
return tname;
}

Public String Gettalias () {
return talias;
}

Public String Gettstyle () {
return tstyle;
}

Public String gettmoral () {
return tmoral;
}

Public String Getthabit () {
return thabit;
}

Public String Gettlooktype () {
return tlooktype;
}

Public String Gettdiscribe () {
return tdiscribe;
}

public void Settid (int tid) {
Tid = tid;
}

public void Settdiscribe (String tdiscribe) {
Tdiscribe = Tdiscribe;
}

public void Settlooktype (String tlooktype) {
Tlooktype = Tlooktype;
}

public void Setthabit (String thabit) {
Thabit = Thabit;
}

public void Settmoral (String tmoral) {
This. Tmoral = tmoral;
}

public void Settstyle (String tstyle) {
Tstyle = Tstyle;
}

public void Settalias (String talias) {
Talias = Talias;
}

public void Settname (String tname) {
Tname = Tname;
}

public void Setttype (String ttype) {
Ttype = Ttype;
}
private int Tid;
Private String Ttype;
Private String Tname;
Private String Talias;
Private String Tstyle;
Private String tmoral;
Private String Thabit;
Private String Tlooktype;
Private String Tdiscribe;
{
Ttype= "";
Talias= "";
Tmoral= "";
}
}

Invoking the SQLite database in Android

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.