Java 在android中将CSV数据导入SQLite数据库时出现问题
我想将包含两个数据表的CSV文件导入SQlite数据库:一个是account_master,另一个是category_master。Java 在android中将CSV数据导入SQLite数据库时出现问题,java,android,sqlite,csv,Java,Android,Sqlite,Csv,我想将包含两个数据表的CSV文件导入SQlite数据库:一个是account_master,另一个是category_master。 我的代码如下 public class MainActivityextends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedIns
我的代码如下
public class MainActivityextends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_setting);
mButtonImport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
file = new File(exportDir, "DemoFile.csv");
try {
CSVReader reader = new CSVReader(new FileReader(file));
String [] nextLine;
try {
while ((nextLine = reader.readNext()) != null) {
String acctitle=nextLine[1];
String accname=nextLine[2];
int value=mDatabaseConnectionAPI.addAccountData(new Account_Masters(acctitle,accname));
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
帐户\u Masters.java
import com.j256.ormlite.field.DatabaseField;
public class Account_Masters implements Serializable{
@DatabaseField(generatedId=true)
int acc_id;
@DatabaseField
String acc_title;
@DatabaseField
String acc_name;
public int getAcc_id() {
return acc_id;
}
public void setAcc_id(int acc_id) {
this.acc_id = acc_id;
}
public Account_Masters() {
// TODO Auto-generated constructor stub
}
public Account_Masters(int accid, String acctitle, String accname) {
// TODO Auto-generated constructor stub
this.acc_id=accid;
this.acc_name=accname;
this.acc_title=acctitle;
}
public Account_Masters(String acctitle, String accname) {
// TODO Auto-generated constructor stub
this.acc_name=accname;
this.acc_title=acctitle;
}
public String getAcc_title() {
return acc_title;
}
public void setAcc_title(String acc_title) {
this.acc_title = acc_title;
}
public String getAcc_name() {
return acc_name;
}
public void setAcc_name(String acc_name) {
this.acc_name = acc_name;
}
public class Category_Master implements Serializable{
@DatabaseField(generatedId=true)
int cat_id;
@DatabaseField
String acc_id;
@DatabaseField
String cat_name;
@DatabaseField
String cat_type;
@DatabaseField
String cat_mode;
public Category_Master() {
// TODO Auto-generated constructor stub
}
public Category_Master(String acc_id,String cat_name,String cat_type,String cat_mode)
{
this.acc_id=acc_id;
this.cat_name=cat_name;
this.cat_type=cat_type;
this.cat_mode=cat_mode;
}
public int getCat_id() {
return cat_id;
}
public void setCat_id(int cat_id) {
this.cat_id = cat_id;
}
public String getCat_name() {
return cat_name;
}
public void setCat_name(String cat_name) {
this.cat_name = cat_name;
}
public String getCat_type() {
return cat_type;
}
public void setCat_type(String cat_type) {
this.cat_type = cat_type;
}
public String getCat_mode() {
return cat_mode;
}
public void setCat_mode(String cat_mode) {
this.cat_mode = cat_mode;
}
public String getAcc_id() {
return acc_id;
}
public void setAcc_id(String acc_id) {
this.acc_id = acc_id;
}
}
public class DatabaseConnectionAPI extends OrmLiteSqliteOpenHelper {
// The Android's default system path of your application database.
// /mnt/sdcard/
private final static String DB_PATH = "/data/data/pkg.android.rootways.rootmoney/databases/";
// private final static String DB_PATH ="/mnt/sdcard/";
private final static String DB_NAME = "RootMoney.sqlite";
private final Context myContext;
public static SQLiteDatabase db;
private RuntimeExceptionDao<Account_Masters, String> personRuntimeDao=null;
private RuntimeExceptionDao<Category_Master, String> categoryRuntimeDao=null;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DatabaseConnectionAPI(Context context) {
super(context, DB_PATH + DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
// e.printStackTrace();
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
// public boolean delete(String sql) {
//
//
// db.execSQL(sql);
// return false;
//
// }
/**
* Copies your database from your local assets-0folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
try {
db.close();
} catch (Exception e) {
System.out.println("no database connected to close");
}
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
System.out.println("Databse opened...." + db);
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public RuntimeExceptionDao<Account_Masters, String> getPersonDataDao() {
Log.v("s", "getTimeDataDao call");
if (personRuntimeDao == null) {
personRuntimeDao = getRuntimeExceptionDao(Account_Masters.class);
}
return personRuntimeDao;
}
public int addAccountData(Account_Masters project)
{
Log.v("Da", "addPersonData call");
RuntimeExceptionDao<Account_Masters, String> dao = getPersonDataDao();
int i = dao.create(project);
return i;
}
}
类别_Master.java
import com.j256.ormlite.field.DatabaseField;
public class Account_Masters implements Serializable{
@DatabaseField(generatedId=true)
int acc_id;
@DatabaseField
String acc_title;
@DatabaseField
String acc_name;
public int getAcc_id() {
return acc_id;
}
public void setAcc_id(int acc_id) {
this.acc_id = acc_id;
}
public Account_Masters() {
// TODO Auto-generated constructor stub
}
public Account_Masters(int accid, String acctitle, String accname) {
// TODO Auto-generated constructor stub
this.acc_id=accid;
this.acc_name=accname;
this.acc_title=acctitle;
}
public Account_Masters(String acctitle, String accname) {
// TODO Auto-generated constructor stub
this.acc_name=accname;
this.acc_title=acctitle;
}
public String getAcc_title() {
return acc_title;
}
public void setAcc_title(String acc_title) {
this.acc_title = acc_title;
}
public String getAcc_name() {
return acc_name;
}
public void setAcc_name(String acc_name) {
this.acc_name = acc_name;
}
public class Category_Master implements Serializable{
@DatabaseField(generatedId=true)
int cat_id;
@DatabaseField
String acc_id;
@DatabaseField
String cat_name;
@DatabaseField
String cat_type;
@DatabaseField
String cat_mode;
public Category_Master() {
// TODO Auto-generated constructor stub
}
public Category_Master(String acc_id,String cat_name,String cat_type,String cat_mode)
{
this.acc_id=acc_id;
this.cat_name=cat_name;
this.cat_type=cat_type;
this.cat_mode=cat_mode;
}
public int getCat_id() {
return cat_id;
}
public void setCat_id(int cat_id) {
this.cat_id = cat_id;
}
public String getCat_name() {
return cat_name;
}
public void setCat_name(String cat_name) {
this.cat_name = cat_name;
}
public String getCat_type() {
return cat_type;
}
public void setCat_type(String cat_type) {
this.cat_type = cat_type;
}
public String getCat_mode() {
return cat_mode;
}
public void setCat_mode(String cat_mode) {
this.cat_mode = cat_mode;
}
public String getAcc_id() {
return acc_id;
}
public void setAcc_id(String acc_id) {
this.acc_id = acc_id;
}
}
public class DatabaseConnectionAPI extends OrmLiteSqliteOpenHelper {
// The Android's default system path of your application database.
// /mnt/sdcard/
private final static String DB_PATH = "/data/data/pkg.android.rootways.rootmoney/databases/";
// private final static String DB_PATH ="/mnt/sdcard/";
private final static String DB_NAME = "RootMoney.sqlite";
private final Context myContext;
public static SQLiteDatabase db;
private RuntimeExceptionDao<Account_Masters, String> personRuntimeDao=null;
private RuntimeExceptionDao<Category_Master, String> categoryRuntimeDao=null;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DatabaseConnectionAPI(Context context) {
super(context, DB_PATH + DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
// e.printStackTrace();
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
// public boolean delete(String sql) {
//
//
// db.execSQL(sql);
// return false;
//
// }
/**
* Copies your database from your local assets-0folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
try {
db.close();
} catch (Exception e) {
System.out.println("no database connected to close");
}
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
System.out.println("Databse opened...." + db);
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public RuntimeExceptionDao<Account_Masters, String> getPersonDataDao() {
Log.v("s", "getTimeDataDao call");
if (personRuntimeDao == null) {
personRuntimeDao = getRuntimeExceptionDao(Account_Masters.class);
}
return personRuntimeDao;
}
public int addAccountData(Account_Masters project)
{
Log.v("Da", "addPersonData call");
RuntimeExceptionDao<Account_Masters, String> dao = getPersonDataDao();
int i = dao.create(project);
return i;
}
}
数据库连接API.java
import com.j256.ormlite.field.DatabaseField;
public class Account_Masters implements Serializable{
@DatabaseField(generatedId=true)
int acc_id;
@DatabaseField
String acc_title;
@DatabaseField
String acc_name;
public int getAcc_id() {
return acc_id;
}
public void setAcc_id(int acc_id) {
this.acc_id = acc_id;
}
public Account_Masters() {
// TODO Auto-generated constructor stub
}
public Account_Masters(int accid, String acctitle, String accname) {
// TODO Auto-generated constructor stub
this.acc_id=accid;
this.acc_name=accname;
this.acc_title=acctitle;
}
public Account_Masters(String acctitle, String accname) {
// TODO Auto-generated constructor stub
this.acc_name=accname;
this.acc_title=acctitle;
}
public String getAcc_title() {
return acc_title;
}
public void setAcc_title(String acc_title) {
this.acc_title = acc_title;
}
public String getAcc_name() {
return acc_name;
}
public void setAcc_name(String acc_name) {
this.acc_name = acc_name;
}
public class Category_Master implements Serializable{
@DatabaseField(generatedId=true)
int cat_id;
@DatabaseField
String acc_id;
@DatabaseField
String cat_name;
@DatabaseField
String cat_type;
@DatabaseField
String cat_mode;
public Category_Master() {
// TODO Auto-generated constructor stub
}
public Category_Master(String acc_id,String cat_name,String cat_type,String cat_mode)
{
this.acc_id=acc_id;
this.cat_name=cat_name;
this.cat_type=cat_type;
this.cat_mode=cat_mode;
}
public int getCat_id() {
return cat_id;
}
public void setCat_id(int cat_id) {
this.cat_id = cat_id;
}
public String getCat_name() {
return cat_name;
}
public void setCat_name(String cat_name) {
this.cat_name = cat_name;
}
public String getCat_type() {
return cat_type;
}
public void setCat_type(String cat_type) {
this.cat_type = cat_type;
}
public String getCat_mode() {
return cat_mode;
}
public void setCat_mode(String cat_mode) {
this.cat_mode = cat_mode;
}
public String getAcc_id() {
return acc_id;
}
public void setAcc_id(String acc_id) {
this.acc_id = acc_id;
}
}
public class DatabaseConnectionAPI extends OrmLiteSqliteOpenHelper {
// The Android's default system path of your application database.
// /mnt/sdcard/
private final static String DB_PATH = "/data/data/pkg.android.rootways.rootmoney/databases/";
// private final static String DB_PATH ="/mnt/sdcard/";
private final static String DB_NAME = "RootMoney.sqlite";
private final Context myContext;
public static SQLiteDatabase db;
private RuntimeExceptionDao<Account_Masters, String> personRuntimeDao=null;
private RuntimeExceptionDao<Category_Master, String> categoryRuntimeDao=null;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DatabaseConnectionAPI(Context context) {
super(context, DB_PATH + DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
// e.printStackTrace();
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
// public boolean delete(String sql) {
//
//
// db.execSQL(sql);
// return false;
//
// }
/**
* Copies your database from your local assets-0folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
try {
db.close();
} catch (Exception e) {
System.out.println("no database connected to close");
}
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
System.out.println("Databse opened...." + db);
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public RuntimeExceptionDao<Account_Masters, String> getPersonDataDao() {
Log.v("s", "getTimeDataDao call");
if (personRuntimeDao == null) {
personRuntimeDao = getRuntimeExceptionDao(Account_Masters.class);
}
return personRuntimeDao;
}
public int addAccountData(Account_Masters project)
{
Log.v("Da", "addPersonData call");
RuntimeExceptionDao<Account_Masters, String> dao = getPersonDataDao();
int i = dao.create(project);
return i;
}
}
公共类数据库连接API扩展了OrmLiteSqliteOpenHelper{
//应用程序数据库的Android默认系统路径。
///mnt/sdcard/
私有最终静态字符串DB_PATH=“/data/data/pkg.android.rootways.rootmoney/databases/”;
//私有最终静态字符串DB_PATH=“/mnt/sdcard/”;
私有最终静态字符串DB_NAME=“RootMoney.sqlite”;
私有最终上下文myContext;
公共静态数据库sqlitedb;
私有RuntimeExceptionDao personRuntimeDao=null;
私有运行时除外DAO类别RUNTIMEDAO=null;
/**
*构造函数获取并保留所传递上下文的引用,以便
*访问应用程序资产和资源。
*
*@param上下文
*/
公共数据库连接API(上下文){
super(上下文,DB_路径+DB_名称,null,1);
this.myContext=上下文;
}
/**
*在系统上创建一个空数据库,并用自己的数据库重写它
*数据库。
* */
public void createDataBase()引发IOException{
布尔值dbExist=checkDataBase();
if(dbExist){
//不执行任何操作-数据库已存在
}否则{
//通过调用此方法,空数据库将被创建到
//默认系统路径
//你的应用程序,所以我们可以覆盖它
//数据库与我们的数据库。
这是.getReadableDatabase();
试一试{
copyDataBase();
}捕获(IOE异常){
//e.printStackTrace();
}
}
}
/**
*检查数据库是否已存在,以避免每次重新复制文件
*打开应用程序的时间。
*
*@如果存在则返回true,如果不存在则返回false
*/
私有布尔校验数据库(){
SQLiteDatabase checkDB=null;
试一试{
字符串myPath=DB_PATH+DB_NAME;
checkDB=SQLiteDatabase.openDatabase(myPath,null,
SQLiteDatabase.OPEN_READWRITE);
}catch(sqlitee异常){
//数据库还不存在。
}
if(checkDB!=null){
checkDB.close();
}
return checkDB!=null?true:false;
}
//公共布尔删除(字符串sql){
//
//
//execSQL(sql);
//返回false;
//
// }
/**
*将数据库从本地assets-0文件夹复制到刚创建的
*系统文件夹中的空数据库,从中可以访问和
*已处理。这是通过传输ByTestStream来完成的。
* */
私有void copyDataBase()引发IOException{
//打开本地数据库作为输入流
InputStream myInput=myContext.getAssets().open(DB_NAME);
//刚创建的空数据库的路径
字符串outFileName=DB_路径+DB_名称;
//打开空数据库作为输出流
OutputStream myOutput=新文件OutputStream(outFileName);
//将字节从输入文件传输到输出文件
字节[]缓冲区=新字节[2048];
整数长度;
而((长度=myInput.read(缓冲区))>0){
写入(缓冲区,0,长度);
}
//关闭溪流
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase()引发SQLException{
试一试{
db.close();
}捕获(例外e){
System.out.println(“没有连接到关闭的数据库”);
}
//打开数据库
字符串myPath=DB_PATH+DB_NAME;
db=SQLiteDatabase.openDatabase(myPath,null,
SQLiteDatabase.OPEN_READWRITE);
System.out.println(“数据库打开…”+db);
}
@凌驾
公共同步作废关闭(){
如果(db!=null)
db.close();
super.close();
}
@凌驾
创建公共void(SQLiteDatabase arg0,ConnectionSource arg1){
//TODO自动生成的方法存根
}
@凌驾
public void onUpgrade(SQLiteDatabase arg0、ConnectionSource arg1、int arg2、,
int arg3){
//TODO自动生成的方法存根
}
公共运行时getPersonDataDao()除外{
Log.v(“s”,“getTimeDataDao调用”);
if(personRuntimeDao==null){
personRuntimeDao=getRuntimeExceptionDao(Account\u Masters.class);
}
返回personRuntimeDao;
}
公共int ADDCOUNTDATA(会计硕士项目)
{
Log.v(“Da”、“addPersonData调用”);
RuntimeExceptionDao=getPersonDataDao();
inti=dao.create(项目);
返回i;
}
}
我的CSV文件格式
当我在SQLite数据库中运行上述代码时,它存储如下所示的值
我希望类别数据进入类别主数据,账户数据进入账户主数据。那么,有人知道我该怎么做吗?如果要在一个csv中有两个表,您必须筛选表名。我建议使用2个单独的csv文件。但如果您希望它位于一个文件中,请检查第一个条目是否为number=>valueRow,否则为HeaderRow。如果你有第二个头颅,你的头颅在下一张桌子上。在代码中按顺序处理每个表都必须使用CSV文件。因为一个CSV文件只代表一个数据表。@DanielBo:你有idae吗?我怎么能在一个CSV文件中分隔两个表?是的,我有,我甚至解释了逻辑id使用:)检查第一条注释。@DanielBo:是的,我读过,但我没有得到idae。你说的id是哪个id CSV行id还是头id?