1. android中怎么将.exe文件存入sqlite数据库 并实现下载功能
转成二进制文件,存入,读取按exe格式读取。数据库在本地,为什么还要下载?
2. android 开发里的 SQLite数据库的一段 查询记录总数,谁能讲解下?看不懂
android我没做过,但编程的原理一样吧,这是一个返回值为Long类型的方法,程序是一步一步执行额,第一行是获取数据库,第二行是执行SQL语句,第三个应该是移到最前端去执行(定位吧),第四个是获取结果中的第一行数据,关闭执行(或者说连接吧大概就是这么个意思),最后返回最终结果,个人理解,有出处见谅
3. 如何在Android应用中使用已有的Sqlite数据库
其主要思路是:
1. 把数据库分解成几个asset文件。
2. 当需要打开数据库时,如果数据库不存在,就把那几个asset文件重新合并成一个数据库文件。
3. 如果数据库的版本改变了,就在onUpgrade()方法中把数据库文件删除掉。
下面是代码:
//数据库的缺省路径
private static finalString DB_PATH = "/data/data/com.mypackage.myapp/databases/";
private static finalString DB_NAME = "mydb.db";
private static finalint DB_VERSION = 2;
private static finalString DB_SPLIT_NAME = "mydb.db.00";
private static finalint DB_SPLIT_COUNT = 3;
private SQLiteDatabasem_database;
private final Contextm_context;
/**
* Constructor
*保存传进来的context参数以用来访问应用的asset和资源文件。
* @param context
*/
public MyDB(Contextcontext) {
super(context, DB_NAME, null, DB_VERSION);
this.m_context = context;
}
public static MyDBopenDatabaseReadOnly(Context context) {
MyDB db = new MyDB(context);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.openDataBase(SQLiteDatabase.OPEN_READONLY);
return db;
}
public static MyDBopenDatabaseReadWrite(Context context) {
MyDB db = new MyDB(context);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.openDataBase(SQLiteDatabase.OPEN_READWRITE);
return db;
}
/**
*创建一个空数据库,用来存储你已有的数据库。
*/
public voidcreateDataBase() throws IOException{
boolean dbExist =checkDataBase();
if (dbExist) {
/*
**如果你的数据库的版本改变了,调用这个方法确保在onUpgrade()被调用时
**传进去的是可写的数据库。
*/
SQLiteDatabase db =this.getWritableDatabase();
if (db != null) {
db.close();
}
}
dbExist = checkDataBase();
if (!dbExist) {
try {
/*
** 调用这个方法以确保在缺省路径内产生一个空数据库,以便在其基础上复制我们已有的数据库。
*/
SQLiteDatabase db =this.getReadableDatabase();
if (db != null) {
db.close();
}
copyDataBase();
}
catch (IOException e) {
Log.e("DB", e.getMessage());
throw new Error("Error copyingdatabase");
}
}
}
/**
* 检查数据库是否已存在,以避免重复复制。
* @return true if it exists, false if itdoesn't
*/
private static booleancheckDataBase(){
SQLiteDatabase checkDB = null;
try {
String path = DB_PATH + DB_NAME;
checkDB =SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e){
//database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* 把存在asset文件中的数据库复制的刚创建的空数据库中。
* */
private voidcopyDataBase() throws IOException {
// 刚创建的空数据库的路径
String outFileName = DB_PATH + DB_NAME;
// 打开空数据库
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024*8];
AssetManager assetMgr =m_context.getAssets();
for (int i = 1; i <= DB_SPLIT_COUNT; i++){
// 打开分解的asset文件
String fn = DB_SPLIT_NAME +String.valueOf(i);
InputStream input = assetMgr.open(fn);
//Log.i("DB", "opened" + fn);
int length;
while ((length = input.read(buffer)) >0) {
//Log.i("DB", "read" + String.valueOf(length));
output.write(buffer, 0, length);
//Log.i("DB", "write" + String.valueOf(length));
}
input.close();
}
//Close the streams
output.flush();
output.close();
}
/**
* 打开数据库。
* */
private voidopenDataBase(int flags) throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
m_database =SQLiteDatabase.openDatabase(myPath, null, flags);
}
/**
* 关闭数据库。
* */
@Override
public synchronizedvoid close() {
if (m_database != null)
m_database.close();
super.close();
}
}
@Override
public voidonCreate(SQLiteDatabase db) {
// 不需做任何事
}
/**
* 在数据库版本提高时,删除原有数据库。
* */
@Override
public voidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
m_context.deleteDatabase(DB_NAME);
}
}
4. android怎么把数据存入数据库
把数据放入数据库
通过把ContentValues对象传入instert()方法把数据插入数据库:
// Gets the data repository in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, content);
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(
FeedReaderContract.FeedEntry.TABLE_NAME,
FeedReaderContract.FeedEntry.COLUMN_NAME_NULLABLE,
values);
insert()方法的第一个参数是表名。第二个参数提供了框架中的一个列名,在ContentValues的值是空的时候,框架会向表中插入NULL值(如果这个参数是“null”,那么当没有值时,框架不会向表中插入一行。
从数据库中读取数据
要从数据库中读取数据,就要使用query()方法,你需要给这个方法传入选择条件和你想要获取数据的列。查询结果会在Cursor对象中被返回。
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
FeedReaderContract.FeedEntry._ID,
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED,
...
};
// How you want the results sorted in the resulting Cursor
String sortOrder =
FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED + " DESC";
Cursor c = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
使用Cursor对象的移动方法来查看游标中的一行数据,在开始读取数据之前必须先调用这个方法。通常,应该从调用moveToFirst()方法开始,它会把读取数据的位置放到结果集中第一实体。对于每一行,你可以通过调用Cursor对象的相应的get方法来读取列的值,如果getString()或getLong()方法。对于每个get方法,你必须把你希望的列的索引位置传递给它,你可以通过调用getColumnIndex()或getColumnIndexOrThrow()方法来获取列的索引。例如:
cursor.moveToFirst();
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID)
);
从数据库中删除数据
要从一个表中删除行数据,你需要提供标识行的选择条件。数据API为创建选择条件提供了一种机制,它会防止SQL注入。这中机制把选择条件分成了选择条件和选择参数。条件子句定义了要查看的列,并且还允许你使用组合列来进行筛选。参数是用于跟条件绑定的、用户筛选数据的值。因为这样不会导致像SQL语句一样的处理,所以它避免了SQL注入。
// Define 'where' part of query.
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
// Specify arguments in placeholder order.
String[] selelectionArgs = { String.valueOf(rowId) };
// Issue SQL statement.
db.delete(table_name, selection, selectionArgs);
更新数据库
当你需要编辑数据库值的时候,请使用update()方法。
这个方法在更新数据时会把insert()方法中内容值的语法跟delete()方法中的where语法结合在一起。
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// New value for one column
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title);
// Which row to update, based on the ID
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
String[] selelectionArgs = { String.valueOf(rowId) };
int count = db.update(
FeedReaderDbHelper.FeedEntry.TABLE_NAME,
values,
selection,
selectionArgs);
5. android中Sqlite 怎么获得查询语句所返回数据的行数?
这个是应该是可以用的直接cursor.moveToFirst()就可以了,不需要while
6. Android查询SQLite数据表中共有多少条记录,代码该怎么写啊?
执行query会返回Cursor对象
Cursor cursor = db.query(tableName, columns, selections, selectionArgs, groupBy, having, orderBy);
cursor.getCount()返回记录的条数(行数)。
7. android编程中怎样接收USB接口发来的数据然后保存到SQLite数据库中?谢了
一般只保存图片的路径,没有直接把图片保存进去,我以前也有试过想放图片到SQlite数据库,后来图片才放几张就不能放了。sqlite数据库到达一定大的情况下就
8. 在Android应用程序中使用SQLite数据库以及怎么用
使用Sqlite首先要了解sql基本语句,android有方法封装SQL语句的,推荐网址http://blog.csdn.net/codeeer/article/details/30237597。希望对你有帮助!