'Android/기타 & Tip'에 해당되는 글 7건

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

import sec.voip.sms.service.SmsReceiver;
import sec.voip.sms.ui.Allsms;
import sec.voip.sms.ui.UIConstants;
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.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;

public class SqlHelper extends SQLiteOpenHelper implements UIConstants
{
public static final String DATABASE_PATH = "/data/data/list.chk/databases/";
public final static String DATABASE_NAME = "smsbox.db";
public static final String TABLE_NAME = "Sms";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PHONE_NO = "phoneNo";
public static final String COLUMN_CONTENT = "content";
public static final String COLUMN_TIME = "time";
public static final String COLUMN_THREAD_ID = "thread_id";
public static final String COLUMN_STATUS = "status";
// public static final String COLUMN_SELECTED = "selected";
public SQLiteDatabase dbSqlite;
private final Context myContext;

public SqlHelper(Context context) 
{
super(context, DATABASE_NAME, null, 1);
this.myContext = context;
}

private static final String DB_CREATE = "CREATE TABLE IF NOT EXISTS "
+ " Sms (_id INTEGER PRIMARY KEY autoincrement, "
+ " phoneNo TEXT, content TEXT, thread_id INTEGER, status BOOL,"
+ " time TEXT);";

@Override
public void onCreate(SQLiteDatabase db) 
{
// check if exists and copy database from resource
try 
{
db.execSQL(DB_CREATE);
catch (SQLException e) {
SmsReceiver.writeLog("[SqlHelper] !!!!!!!!!!! onCreate error : "+e.getMessage()+" !!!!!!!!!!!",E);
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
// Log.w(TAG, "[VoIP_SMS]********Upgrading database from version " + oldVersion
// + " to " + newVersion + ", which will destroy all old data");
onCreate(db);
}
public void createDatabase() 
{
createDB();
}

private void createDB() 
{
boolean dbExist = DBExists();
if (!dbExist) {
copyDBFromResource();
}
}

private boolean DBExists() 
{
SQLiteDatabase db = null;
try 
{
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
onCreate(db);
catch (SQLiteException e) 
{
SmsReceiver.writeLog("[SqlHelper] !!!!!!!!!!! DBExists error : "+e.getMessage()+" !!!!!!!!!!!",E);
}

if (db != null) 
{
db.close();
}
return db != null ? true : false;
}

private void copyDBFromResource() 
{
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try 
{
inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;

while ((length = inputStream.read(buffer)) > 0) 
{
outStream.write(buffer, 0, length);
}

outStream.flush();
outStream.close();
inputStream.close();

catch (IOException e) 
{
throw new Error("Problem copying database from resource file.");
}
}

public void openDataBase() throws SQLException 
{
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}

@Override
public synchronized void close() 
{
if (dbSqlite != null)
dbSqlite.close();
super.close();
}

public Cursor getCursor(int i) 
{
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
String[] asColumnsToReturn = new String[] 
{ COLUMN_ID, COLUMN_PHONE_NO, COLUMN_CONTENT, COLUMN_TIME, COLUMN_THREAD_ID, COLUMN_STATUS };
Cursor mCursor = null;
try 
{
mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn,
"thread_id = " + i, null, null, null, "_id DESC");
catch (SQLiteException e) 
{
SmsReceiver.writeLog("[SqlHelper] !!!!!!!!!!! getCursor error : "+e.getMessage()+" !!!!!!!!!!!",E);
}
return mCursor;
}
//20110307 kwon db select colume삭제
// public void clearSelections() 
// {
// ContentValues values = new ContentValues();
// values.put(" selected", 0);
// this.dbSqlite.update(SqlHelper.TABLE_NAME, values, null, null);
// }

private ContentValues getContentValues(Allsms sms) 
{
ContentValues Values = new ContentValues();
Values.put("phoneNo", sms.getPhoneNo());
Values.put("content", sms.getContent());
Values.put("time", sms.getTime());
// Values.put("selected", sms.getSelected());
Values.put("status", sms.getStatus());
Values.put("thread_id", sms.getThread_id());
return Values;
}

public boolean insert(String TableName, Allsms sms) 
{
SQLiteDatabase myDb = this.getWritableDatabase();
myDb.beginTransaction();
try 
{
myDb.insert("Sms", null, getContentValues(sms));
myDb.setTransactionSuccessful();
catch (Exception e) 
{
SmsReceiver.writeLog("[SqlHelper] !!!!!!!!!!! insert error : "+e.getMessage()+" !!!!!!!!!!!",E);
return false;
finally 
{
myDb.endTransaction();
myDb.close();
}
return true;
}

// public boolean deleteSelected() 
// {
// SQLiteDatabase myDb = this.getWritableDatabase();
// myDb.beginTransaction();
// try 
// {
//// int a = myDb.delete("Sms", "selected=1", null);
//// Log.i(TAG, "[SqlHelper]*************** delete result : " + a);
//
// myDb.setTransactionSuccessful();
//
// catch (Exception e) 
// {
// Log.e(TAG,"[SqlHelper] !!!!!!!!!!! deleteSelected error : "+e.getMessage()+" !!!!!!!!!!!");
// return false;
//
// finally 
// {
// myDb.endTransaction();
// myDb.close();
// }
// return true;
// }
}

일부 응용하면 다른 코드에서 추가 사용가능
블로그 이미지

알 수 없는 사용자

안드로이드 개발

,