android保存到資料庫
Ⅰ android 如何獲取保存的圖片的地址 並存到資料庫中
安卓中如何獲取保存的圖片uri 並保存到sqlite資料庫中
有如下兩種方法,僅供參考
方法一:java代碼
public void saveIcon(Bitmap icon) {
if (icon == null) {
return;
}
// 最終圖標要保存到瀏覽器的內部資料庫中,系統程序均保存為SQLite格式,Browser也不例外,因為圖片是二進制的所以使用位元組數組存儲資料庫的
// BLOB類型
final ByteArrayOutputStream os = new ByteArrayOutputStream();
// 將Bitmap壓縮成PNG編碼,質量為100%存儲
icon.compress(Bitmap.CompressFormat.PNG, 100, os);
// 構造SQLite的Content對象,這里也可以使用
raw ContentValues values = new ContentValues();
// 寫入資料庫的
Browser.BookmarkColumns.TOUCH_ICON欄位 values.put(Browser.BookmarkColumns.TOUCH_ICON, os.toByteArray());
DBUtil.update(....);
//調用更新或者插入到資料庫的方法
}
}
方法二:如果數據表入口時一個content:URIJava代碼
import android.provider.MediaStore.Images.Media;
import android.content.ContentValues;
import java.io.OutputStream;
// Save the name and description of an image in a ContentValues map.
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, "road_trip_1");
values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
values.put(Media.MIME_TYPE, "image/jpeg");
// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
OutputStream outStream = getContentResolver().openOutputStream(uri);
sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.close();
} catch (Exception e) {
Log.e(TAG, "exception while writing image", e);
}
原文請看http://www.bafenbaosoft.com/post/48.html
Ⅱ android怎麼將數據存入資料庫
你通過getText()方法首先得到輸入的值,然後調用資料庫的插入方法 db.insert();插入到資料庫中就行 就想這樣 x0dx0aEditText et ; x0dx0aString num = et.getText().toString(); x0dx0apublic void addData(String num) { x0dx0a SQLiteDatabase db = dbHelper.getWritableDatabase(); x0dx0a ContentValues values = new ContentValues(); x0dx0a values.put("num", num); x0dx0a db.insert("表名", null, values); x0dx0a } x0dx0a x0dx0a當你調用這個 addData()方法時就會向資料庫中插入數據了
Ⅲ android百度消息推送過來的數據如何存入資料庫
可以通過getText()方法,首先需要的到輸入的值,然後調用資料庫的插入方法 db.insert();插入到資料庫中就行;這樣數據就存入資料庫了。消息推送的行業應用
1、廣告推送
信息推送最熱門的應用方向是廣告推送,也就是互聯網效果營銷的應用方向。
2、社區信息
大量的web2.0社區,也激發了社區信息的推送應用。基於用戶關系、用戶行為,給用戶推送用戶感興趣的信息,包括帖子、任務、游戲,等等。
對於消息推送軟體,推薦使用深圳極光家的消息推送軟體。極光是國內領先的移動開發者服務提供商,極光會根據前者的年齡、性別和興趣等標簽幫助企業設計廣告頁面,並在網路、騰訊、阿里媽媽、今日頭條等多家主流媒體上通過廣告的方式向用戶投放具有針對性和吸引力的促銷優惠信息。
Ⅳ 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);
Ⅳ android開發怎麼存儲數據
Android平台開發過程中,進行數據存儲的方式有五種,一下是這五種數據存儲的總結:
第一種:使用SharedPreferences存儲數據
適用范圍:保存少量的數據,且這些數據的格式非常簡單:字元串型、基本類型的值。比如應用程序的各種配置信息(如是否打開音效、是否使用震動效果、小游戲的玩家積分等),解鎖口 令密碼等
核心原理:保存基於XML文件存儲的key-value鍵值對數據,通常用來存儲一些簡單的配置信息。通過DDMS的File Explorer面板,展開文件瀏覽樹,很明顯SharedPreferences數據總是存儲在/data/data/<package name>/shared_prefs目錄下。SharedPreferences對象本身只能獲取數據而不支持存儲和修改,存儲修改是通過SharedPreferences.edit()獲取的內部介面Editor對象實現。SharedPreferences本身是一 個介面,程序無法直接創建SharedPreferences實例,只能通過Context提供的getSharedPreferences(String name, int mode)方法來獲取SharedPreferences實例,該方法中name表示要操作的xml文件名,第二個參數具體如下:
Context.MODE_PRIVATE: 指定該SharedPreferences數據只能被本應用程序讀、寫。
Context.MODE_WORLD_READABLE:指定該SharedPreferences數據能被其他應用程序讀,但不能寫。
Context.MODE_WORLD_WRITEABLE:指定該SharedPreferences數據能被其他應用程序讀,寫
Editor有如下主要重要方法:
SharedPreferences.Editor clear():清空SharedPreferences里所有數據
SharedPreferences.Editor putXxx(String key , xxx value):向SharedPreferences存入指定key對應的數據,其中xxx 可以是boolean,float,int等各種基本類型據
SharedPreferences.Editor remove():刪除SharedPreferences中指定key對應的數據項
boolean commit():當Editor編輯完成後,使用該方法提交修改
實際案例:運行界面如下
Android sqlite3資料庫管理工具
Android SDK的tools目錄下提供了一個sqlite3.exe工具,這是一個簡單的sqlite資料庫管理工具。開發者可以方便的使用其對sqlite資料庫進行命令行的操作。
程序運行生成的*.db文件一般位於"/data/data/項目名(包括所處包名)/databases/*.db",因此要對資料庫文件進行操作需要先找到資料庫文件:
1、進入shell 命令
adb shell
2、找到資料庫文件
#cd data/data
#ls --列出所有項目
#cd project_name --進入所需項目名
#cd databases
#ls --列出現寸的資料庫文件
3、進入資料庫
#sqlite3 test_db --進入所需資料庫
會出現類似如下字樣:
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
至此,可對資料庫進行sql操作。
4、sqlite常用命令
>.databases --產看當前資料庫
>.tables --查看當前資料庫中的表
>.help --sqlite3幫助
>.schema --各個表的生成語句
以上便是android開發存儲數據的五種方法!
Ⅵ android怎麼將數據存入資料庫
你通過getText()方法首先得到輸入的值,然後調用資料庫的插入方法 db.insert();插入到資料庫中就行 就想這樣
EditText et ;
String num = et.getText().toString();
public void addData(String num) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("num", num);
db.insert("表名", null, values);
}
當你調用這個 addData()方法時就會向資料庫中插入數據了
Ⅶ 安卓中如何使用intentservice請求數據並保存在資料庫中
service類必須實現一個接收方法,接收中傳遞的是intent
@Override
public
IBinder
onBind(Intent
intent)
{
Bundle
bundle
=
intent.getExtras();
String
stringVal
=
bundle.getString("stringValue");
//用於接收字元串
int
numVal
=
bundle.getInt("intValue");
//用於接收int類型數據
byte[]
bytes
=
bundle.getByteArray("bytesValue");
//用於接收位元組流,你可以把文件放入位元組流
return
null;
}
你可以用Bundle來接受你從Activity發過來的數據,然後使用Bundle提供各個方法來接受數據。
如果僅僅是字元串之類的,
使用getStringExtra方法直接接收即可。
@Override
public
IBinder
onBind(Intent
intent)
{
String
str1
=
intent.getStringExtra("str1");
String
str2
=
intent.getStringExtra("str2");
return
null;
}