我需要使用什么样的查询
“创建表 CallDB 为(从 CallLog.Calls 中选择 *)”
我想将整个通话记录存储到另一个表中以进行处理。因为我读过 taht calllog.calls 只有最后 500 条记录
好的,我自己做了一个解决方案。不知道它是否最好但仍然
package database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.provider.CallLog;
import android.util.Log;
import android.widget.Toast;
public class CallDBDatasource
{
Context context;
private SQLiteDatabase db;
private CallDBHelper dbHelper;
public CallDBDatasource(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
dbHelper = new CallDBHelper(context);
}
public void open() throws SQLException {
db = dbHelper.getWritableDatabase();
Log.d("CallDBDatasource", "insert check");
Toast t = Toast.makeText(context, "try... " , Toast.LENGTH_SHORT);
t.show();
Cursor callInitCheck = db.rawQuery("select count(*) from call", null);
Toast t2a = Toast.makeText(context, "count rows " + callInitCheck.getCount() , Toast.LENGTH_SHORT);
t2a.show();
callInitCheck.moveToNext();
if( Integer.parseInt( callInitCheck.getString(0)) == 0) // if no rows then do
{
// if empty then insert into call
// select * from CallLog.Calls
String[] allCol = {
CallLog.Calls.CACHED_NAME ,
CallLog.Calls.CACHED_NUMBER_LABEL ,
CallLog.Calls.CACHED_NUMBER_TYPE ,
CallLog.Calls.DATE ,
CallLog.Calls.DURATION ,
CallLog.Calls.TYPE ,
CallLog.Calls.NUMBER
};
Cursor c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, allCol , null,null, null);
if(c.moveToFirst())
{
Toast t3 = Toast.makeText(context, "count " + c.getColumnCount() , Toast.LENGTH_LONG);
t3.show();
Log.d("CallDBDatasource", "inside cursor");
CallDB callDB = new CallDB();
int indexCACHED_NAME = c.getColumnIndex(CallLog.Calls.CACHED_NAME);
int indexCACHED_NUMBER_LABEL = c.getColumnIndex(CallLog.Calls.CACHED_NUMBER_LABEL);
int indexCACHED_NUMBER_TYPE = c.getColumnIndex(CallLog.Calls.CACHED_NUMBER_TYPE);
int indexDATE = c.getColumnIndex(CallLog.Calls.DATE);
int indexDURATION = c.getColumnIndex(CallLog.Calls.DURATION);
int indexNUMBER = c.getColumnIndex(CallLog.Calls.TYPE);
int indexTYPE = c.getColumnIndex(CallLog.Calls.NUMBER);
do
{
callDB.setCACHED_NAME(c.getString(indexCACHED_NAME));
callDB.setCACHED_NUMBER_LABEL(c.getString(indexCACHED_NUMBER_LABEL));
callDB.setCACHED_NUMBER_TYPE(c.getString(indexCACHED_NUMBER_TYPE));
callDB.setDATE(c.getString(indexDATE));
callDB.setDURATION(c.getString(indexDURATION));
callDB.setTYPE(c.getString(indexNUMBER));
callDB.setNUMBER(c.getString(indexTYPE));
insert(callDB);
}
while(c.moveToNext());
Toast t1 = Toast.makeText(context, "added " + c.getCount() , Toast.LENGTH_SHORT);
t1.show();
//close cursor
c.close();
} // end if cursor
Log.d("CallDBDatasource", "INITED :)");
} // end if
callInitCheck.close();
} // end open()
public void close() {
dbHelper.close();
Log.d("CallDBDatasource", "close()");
} // end close()
public void insert(CallDB callDB) {
ContentValues values = new ContentValues();
values.put(CallDBHelper.CACHED_NAME, callDB.getCACHED_NAME());
values.put(CallDBHelper.CACHED_NUMBER_LABEL, callDB.getCACHED_NUMBER_LABEL());
values.put(CallDBHelper.CACHED_NUMBER_TYPE, callDB.getCACHED_NUMBER_TYPE());
values.put(CallDBHelper.DATE, callDB.getDATE());
values.put(CallDBHelper.DURATION, callDB.getDURATION());
values.put(CallDBHelper.NUMBER, callDB.getNUMBER());
values.put(CallDBHelper.TYPE, callDB.getTYPE());
db.insert(CallDBHelper.DATABASE_TABLE, "" , values);
Log.d("CallDBDatasource", "values inserted");
} // end insert
public void update(CallDB callDB) {
ContentValues values = new ContentValues();
values.put(CallDBHelper.CACHED_NAME, callDB.getCACHED_NAME());
values.put(CallDBHelper.CACHED_NUMBER_LABEL, callDB.getCACHED_NUMBER_LABEL());
values.put(CallDBHelper.CACHED_NUMBER_TYPE, callDB.getCACHED_NUMBER_TYPE());
values.put(CallDBHelper.DATE, callDB.getDATE());
values.put(CallDBHelper.DURATION, callDB.getDURATION());
values.put(CallDBHelper.NUMBER, callDB.getNUMBER());
values.put(CallDBHelper.TYPE, callDB.getTYPE());
db.update(CallDBHelper.DATABASE_TABLE, values , null, null);
Log.d("CallDBDatasource", "values updated");
} // end update
public long cIn() {
long ret = 0L;
Cursor c = db.rawQuery("select sum(DURATION) from call where TYPE = ?", new String[] { String.valueOf(CallLog.Calls.INCOMING_TYPE) });
if(c.moveToNext())
{
try {
Toast t = Toast.makeText(context, "" + c.getString(0) , Toast.LENGTH_SHORT);
t.show();
ret = Long.parseLong(c.getString(0));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.close();
return ret;
} // end cIn()
public long cOut() {
long ret = 0L;
Cursor c = db.rawQuery("select sum(DURATION) from call where TYPE = ?", new String[] { String.valueOf(CallLog.Calls.OUTGOING_TYPE) });
if(c.moveToNext())
{
try {
Toast t = Toast.makeText(context, "" + c.getString(0) , Toast.LENGTH_SHORT);
t.show();
ret = Long.parseLong(c.getString(0));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.close();
return ret;
} // end cOut()
public long cMiss() {
long ret = 0L;
Cursor c = db.rawQuery("select count(*) from call where TYPE = ?", new String[] { String.valueOf(CallLog.Calls.MISSED_TYPE) });
if(c.moveToNext())
{
try {
Toast t = Toast.makeText(context, "" + c.getString(0) , Toast.LENGTH_SHORT);
t.show();
ret = Long.parseLong(c.getString(0));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
c.close();
return ret;
} // end cMiss()
} // end class
package database;
public class CallDB {
private String ID = null ;
private String CACHED_NAME = null;
private String CACHED_NUMBER_LABEL = null ;
private String CACHED_NUMBER_TYPE = null ;
private String DATE = null ;
private String DURATION = null ;
private String NUMBER = null ;
private String TYPE = null ;
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getCACHED_NAME() {
return CACHED_NAME;
}
public void setCACHED_NAME(String cACHED_NAME) {
CACHED_NAME = cACHED_NAME;
}
public String getCACHED_NUMBER_LABEL() {
return CACHED_NUMBER_LABEL;
}
public void setCACHED_NUMBER_LABEL(String cACHED_NUMBER_LABEL) {
CACHED_NUMBER_LABEL = cACHED_NUMBER_LABEL;
}
public String getCACHED_NUMBER_TYPE() {
return CACHED_NUMBER_TYPE;
}
public void setCACHED_NUMBER_TYPE(String cACHED_NUMBER_TYPE) {
CACHED_NUMBER_TYPE = cACHED_NUMBER_TYPE;
}
public String getDATE() {
return DATE;
}
public void setDATE(String dATE) {
DATE = dATE;
}
public String getDURATION() {
return DURATION;
}
public void setDURATION(String dURATION) {
DURATION = dURATION;
}
public String getNUMBER() {
return NUMBER;
}
public void setNUMBER(String nUMBER) {
NUMBER = nUMBER;
}
public String getTYPE() {
return TYPE;
}
public void setTYPE(String tYPE) {
TYPE = tYPE;
}
public CallDB() {
// TODO Auto-generated constructor stub
}
}
package database;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.provider.CallLog;
import android.util.Log;
import android.widget.Toast;
public class CallDBHelper extends SQLiteOpenHelper {
Context context;
public static final String DATABASE_NAME = "AndroidLoggerCall" ;
public static final String DATABASE_TABLE = "call" ;
public static final int DATABASE_VERSION = 1 ;
// rows
public static final String ID = "_id";
public static final String CACHED_NAME = "CACHED_NAME";
public static final String CACHED_NUMBER_LABEL = "CACHED_NUMBER_LABEL";
public static final String CACHED_NUMBER_TYPE = "CACHED_NUMBER_TYPE";
public static final String DATE = "DATE";
public static final String DURATION = "DURATION";
public static final String NUMBER = "NUMBER";
public static final String TYPE = "TYPE";
// end rows
// create table string
public static final String CREATE_TABLE = "create table "
+ DATABASE_TABLE + "("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
+ CACHED_NAME + " text,"
+ CACHED_NUMBER_LABEL + " text,"
+ CACHED_NUMBER_TYPE + " text,"
+ DATE + " text,"
+ DURATION + " text,"
+ NUMBER + " text,"
+ TYPE + " text"
+ ");" ;
// end create table
public CallDBHelper(Context context) {
super(context, DATABASE_NAME , null, DATABASE_VERSION);
this.context = context;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.d("CallDBHelper", "onCreate");
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}