Android puts dB files under sdcard for access

Source: Internet
Author: User

This article will introduce

How to add an expected sqllite database, and perform operations such as query in the database

Previous Article introduced how to add data in SD card

In this case, we will only explain how to add new resources and directly explain the actions of the newly added and queried resources.

Public class db_sample {public static final string str_table_name = "table_name"; // table name ‑ public static final string str_field_id = "field_id "; // The specified bit name must public static final string str_field_name = "field_name"; // The specified bit name must Private Static Context context; Public db_sample (context) {This. context = context; // when the new class is used, it must be written into the context, because openorcreatedatabase is created by the method under the context} // The method pub of the table LIC static final void fncreatedb () {// mode_world_writeable, only permission for sqlitedatabase dbwrite = context. openorcreatedatabase ("/sdcard/hi_test.db", context. mode_world_writeable, null); // create table string SQL = string. format ("create table % s (% s integer primary key autoincrement, % s text)", str_table_name, str_field_id, str_field_name); dbwrite.exe csql (SQL );} // method public static final long F Ninsert (string strtext) {sqlitedatabase dbwrite = context. openorcreatedatabase ("/sdcard/hi_test.db", context. mode_world_writeable, null); contentvalues CV = new contentvalues (); cv. put (str_field_name, strtext); Return dbwrite. insert (str_table_name, null, CV); // if return-1 indicates that there is already exist, otherwise, the response will be returned in the response column} // method public static final int fnread (string strtext) {// mode_world_writeable, only sqlitedat permission can be obtained from the token. Abase dbread = context. openorcreatedatabase ("/sdcard/hi_test.db", context. mode_world_readable, null); cursor = dbread. rawquery ("select" + str_field_id + "from" + str_table_name + "where" + str_field_name + "=? ", New string [] {strtext}); If (cursor. movetofirst () {return cursor. getint (0) ;}return-1 ;}}




The call method is as follows:

public class test extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); db_sample db = new db_sample(this); db.fnCreateDB(); db.fnInsert("HI"); int iID = db.fnRead("HI"); }}

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.