The table was not found on the Android database

Source: Internet
Author: User

Build a table of your own and put it in the assets directory

Package mine;

Import Android.annotation.SuppressLint;
Import android.content.ContentValues;
Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteDatabase.CursorFactory;
Import android.database.sqlite.SQLiteException;
Import Android.database.sqlite.SQLiteOpenHelper;
Import Android.widget.Toast;

Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;

public class Mydatebasehelper extends Sqliteopenhelper {
public static final String Create_book = "CREATE table User ("
+ "ID integer primary key autoincrement,"
+ "username text not NULL,"
+ "Password text NOT NULL,"
+ "Sex text NOT NULL,"
+ "number text not null);";

Private Context Mcontext;

Version of the user database file
private static final int db_version = 1;
The database file destination is the system default location, Cn.arthur.examples is your package name
private static String db_path= "/data/data/com.example.pinan/databases/";
Private String DBPath;

The following two static variables are the name of the target file and the file name under the Assets folder, respectively
private static String db_name = "huawei.db";
private static String Assets_name = "huawei.db";


Public Mydatebasehelper (context context, String name,cursorfactory factory, int version) {
Super (context, name, Factory, version);
TODO auto-generated Constructor stub
Mcontext = context;
try {
CreateDatabase ();
} catch (IOException e) {
E.printstacktrace ();
}
}

@Override
public void OnCreate (Sqlitedatabase db) {
TODO auto-generated Method Stub

Db.execsql (Create_book);
Db.execsql ("INSERT into userdate (Nametext,password) VALUES (' 14 ', ' 14 ')");
}

@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
TODO auto-generated Method Stub
Db.execsql ("drop table if exists user");
OnCreate (DB);
}

public void CreateDatabase () throws IOException {
DBPath = Mcontext.getfilesdir (). GetPath (). Split ("Files") [0] + "databases/";
Boolean dbexist = Checkdatabase ();
if (dbexist)
{
Database already exists, no action done

}
Else
{
Create a database
try {
File dir = new file (DBPath);
if (!dir.exists ()) {
Dir.mkdirs ();
}
File DBF = new file (DBPath + db_name);
if (dbf.exists ()) {
Dbf.delete ();
}
Sqlitedatabase.openorcreatedatabase (DBF, NULL);
Copy the database files in the asseets to Db_path
Copydatabase ();
} catch (IOException e) {
throw new Error ("Database creation failed");
}
}
}

Check that the database is valid
Private Boolean checkdatabase () {
Sqlitedatabase CheckDB = null;
String MyPath = DBPath + db_name;
try{
CheckDB = sqlitedatabase.opendatabase (MyPath, NULL, sqlitedatabase.open_readonly);
}catch (Sqliteexception e) {
Database does ' t exist yet.
}
if (CheckDB! = null) {
Checkdb.close ();
}
return CheckDB! = null? True:false;
}

/**
* Copy the database from the assets file to the specified path
* Copy using the input/output stream
**/
private void Copydatabase () throws ioexception{

InputStream myinput = Mcontext.getassets (). open (Assets_name);
String outfilename = DBPath + db_name;
OutputStream myoutput = new FileOutputStream (outfilename);
byte[] buffer = new byte[1024];
int length;
while (length = myinput.read (buffer)) >0) {
Myoutput.write (buffer, 0, length);
}
Myoutput.flush ();
Myoutput.close ();
Myinput.close ();
}

}

The table was not found on the Android database

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.