如何使用按钮将我的tblrecords导出到csv文件中?帮帮人:(
package com.example.rodgie.atry;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by rodgie on 1/1/2009.
*/
/**
* This is all my database
*/
/**
* I want to export all records into tblrecords in csv file, using button
*/
public class MyDbHelpers {
//Database ng records
public static final String tblrecord = "record";
public static final String id = "_id";
public static final String name = "name";
public static final String middlename= "middle";
public static final String lastname="lastname";
public static final String date ="date";
public static final String time="time";
//table para sa user
public static final String tableuser = "users";
public static final String uid = "uid";
public static final String username = "username";
public static final String password = "password";
public MyDbHelpers.DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context ctx;
private static final String TAG = "MyDbHelper";
static final String DB_NAME = "rodgie.db";
static final int DB_VERSION = 2;
/**
* i want my tblrecords export into csv file using button
*/
private static final String CREATE_TABLE = "create table " + tblrecord + "(" + id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + name + " String(200), " + middlename + " String(100), " + lastname + " String(100), "+date + " String(100), "+time+" String(100));";
// my table in accounts
private static final String CREATE_TABLE2 = "create table " + tableuser + "(" + uid + " INTEGER PRIMARY KEY AUTOINCREMENT, " + username + " String(100), " + password + " String(100));";
public static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
Log.w(TAG, CREATE_TABLE);
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_TABLE2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + tblrecord);
onCreate(db);
}
}
public MyDbHelpers(Context context)
{
this.ctx = context;
}
//for add visitors
public long insertData(String una, String pangalawa, String pangatlo, String araw, String oras)
{
ContentValues initialValues = new ContentValues();
initialValues.put(name, una);
initialValues.put(middlename, pangalawa);
initialValues.put(lastname, pangatlo);
initialValues.put(date, araw);
initialValues.put(time, oras);
return mDb.insert(tblrecord, null, initialValues);
}
//para sa log in
public long insertUser(String user, String pass)
{
ContentValues vil = new ContentValues();
vil.put(username, user);
vil.put(password, pass);
return mDb.insert(tableuser, null, vil);
}
public MyDbHelpers open() throws SQLException
{
mDbHelper = new MyDbHelpers.DatabaseHelper(ctx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close()
{
if (mDbHelper != null)
{
mDbHelper.close();
}
}
public boolean deleteAllRecord()
{
int doneDelete = 0;
doneDelete = mDb.delete(tblrecord, null , null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
public Cursor fetchRecordByName(String inputText) throws SQLException
{
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0)
{
mCursor = mDb.query(tblrecord, new String[] {id, name, middlename, lastname, date, time}, null, null, null, null, null);
}
else
{
mCursor = mDb.query(true, tblrecord, new String[] {id, name, middlename, lastname, date, time}, name + " like '%" + inputText + "%'", null, null, null, null, null);
}
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAllRecord()
{
Cursor mCursor = mDb.query(tblrecord, new String[] {id, name, middlename, lastname, date, time}, null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public void insertSomeRecord()
{
}
public int selectRecord()
{
String count = "SELECT * FROM " + tblrecord;
mDb = mDbHelper.getReadableDatabase();
Cursor cursor = mDb.rawQuery(count, null);
int i = cursor.getCount();
cursor.close();
return i;
}
public int getUsers()
{
String count = "SELECT * FROM " + tableuser;
mDb = mDbHelper.getReadableDatabase();
Cursor cursor = mDb.rawQuery(count, null);
int i = cursor.getCount();
cursor.close();
return i;
}
public void deleteUsers()
{
mDb = mDbHelper.getReadableDatabase();
mDb.delete(tableuser, uid + "!= 0", null);
}
public void setUsername(String a, String b)
{
mDb = mDbHelper.getReadableDatabase();
ContentValues content = new ContentValues();
content.put(username, a);
mDb.update(tableuser, content, username + " = '" + b + "'", null);
}
//bagong username ko to
public boolean getUsername(String as)
{
String arg = "select " + username + " from " + tableuser + " where " + username + " = '" + as + "';";
mDb = mDbHelper.getReadableDatabase();
Cursor c = mDb.rawQuery(arg, null);
int eger = c.getCount();
c.close();
if(eger > 0)
{
return true;
}
else
{
return false;
}
}
//bagong password
public void setPassword(String newa, String old)
{
mDb = mDbHelper.getReadableDatabase();
ContentValues content = new ContentValues();
content.put(password, newa);
mDb.update(tableuser, content, password + " = '" + old + "'", null);
}
//pagkuha ng password
public boolean getPassword(String old)
{
String arg = "select " + password + " from " + tableuser + " where " + password + " = '" + old + "';";
mDb = mDbHelper.getReadableDatabase();
Cursor c = mDb.rawQuery(arg, null);
int eger = c.getCount();
c.close();
if(eger > 0)
{
return true;
}
else
{
return false;
}
}
//signin lang muna to
public boolean logingIn(String bag1, String bag2)
{
String arg = "select " + username + ", " + password + " from " + tableuser + " where " + username + " = '" + bag1 + "' and " + password + " = '" + bag2 + "';";
mDb = mDbHelper.getReadableDatabase();
Cursor c = mDb.rawQuery(arg, null);
int eger = c.getCount();
c.close();
if(eger > 0)
{
return true;
}
else
{
return false;
}
}
//para sa update ng user
public void updateusers(String oldName, String newName, String oldPass, String newPass)
{
mDb = mDbHelper.getWritableDatabase();
ContentValues args = new ContentValues();
args.put(username, newName);
args.put(password, newPass);
mDb.update(tableuser, args, username + " = '" + oldName + "' and " + password + " = '" + oldPass + "'" , null);
}
//delete ko to
public void deleteARecord(int fg)
{
mDb = mDbHelper.getReadableDatabase();
mDb.delete(tblrecord, id + " = " + fg, null);
}
//update ko to
public void update(int a, String uname, String urealStocks, String ucurrentStocks, String uauthor, String ugenre)
{
mDb = mDbHelper.getReadableDatabase();
ContentValues f = new ContentValues();
f.put(name, uname);
f.put(middlename, urealStocks);
f.put(lastname, ucurrentStocks);
f.put(date, uauthor);
f.put(time, ugenre);
mDb.update(tblrecord, f, id + " = " + a, null);
}
}
答案 0 :(得分:0)
好的,这是一个快速的"不理想的"如何实现这一点的例子
public class User{
//properties
private String userName;
private String password;
//constructor
public User(String name, String password){
this.userName = name;
this.password = password;
}
//getters and setters
public String getUserName(){return userName;};
public String getPassword(){return password;};
public void setUserName(String userName){this.userName = userName;}
public void setPassword(String password){this.password = password;}
}
public List<User> getUsers(){
List<User> users = new ArrayList<>();
String count = "SELECT * FROM " + tableuser;
mDb = mDbHelper.getReadableDatabase();
Cursor cursor = mDb.rawQuery(count, null);
while(cursor.moveToNex()){
//assuming that userName is 1st index and password is 2nd
users.add(new User(cursor.getString(1),cursor.getString(2));
}
return users;
}
private void writeToCsv(List<User> users){
String outputFile = "users.csv";
// before we open the file check to see if it already exists
boolean alreadyExists = new File(outputFile).exists();
try {
// use FileWriter constructor that specifies open for appending
CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');
if (!alreadyExists)
{
csvOutput.write("userName");
csvOutput.write("password");
csvOutput.endRecord();
}
// else assume that the file already has the correct header line
// write out a users to the .csv file
for(User user : users){
csvOutput.write(user.getUserName());
csvOutput.write(user.getPassword());
csvOutput.endRecord();
}
csvOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
writeToCsv(getUsers());
不是一个很好的例子,但我希望它有所帮助。