當前位置:首頁 » 安卓系統 » android資料庫表格資料庫數據

android資料庫表格資料庫數據

發布時間: 2022-08-04 22:57:10

① 在android上顯示mysql資料庫表格數據的小程序

http://www.cnblogs.com/xiao-xu/archive/2013/04/17/3025732.html
這是我自己總結的,有不明白的地方可以問,希望對你有幫助

② android 如何連接mysql資料庫,並且往資料庫裡面插入數據

去看看httpget和httppost,再看一下servlet就可以實現一個簡單的連接了,連接寫在servlet裡面就好

③ android中的資料庫怎麼去寫如何建表,希望有具體代碼參考啊!

android 中的資料庫操作 android 中的應用開發很難避免不去使用資料庫, 聊聊 android 中的數據 庫操作,我發給你 word 貼不下
一、android 內的資料庫的基礎知識介紹
1.用了什麼資料庫
android 中採用的資料庫是 SQLite 這個輕量級的嵌入式開源資料庫,它是用 c 語言構建的。相關簡介可以從鏈接查看。
2.資料庫基本知識觀花
對於一些和我一樣還沒有真正系統學習資料庫技術的同學來說,把 SQL92 標 准中的一些基本概念、基本語句快速的了解一下,是很有必要的,這樣待會用 Android 的 database 相關方法去執行一些資料庫語句時就不會茫然了。
①資料庫的基本結構——表格
表格是資料庫中儲存資料的基本架構。表格被分為欄位 (column) 及列位 (row)。每 一列代表一筆資料,而每一欄代表一筆資料的一部份。舉例來說,如果我們有一個記載顧客 資料的表格,那欄位就有可能包括姓、名、地址、城市、國家、生日..等等。每一個表格 . 擁有一個獨一無二的名字(Table Name)以便能夠讓用戶定位到它上面。一個典型的表格結 構如下:
Store_Information 表格
store_name Sales Date Los Angeles $1500 Jan-05-1999 San Diego $250 Jan-07-1999 Los Angeles $300 Jan-08-1999 Boston $700 Jan-08-1999 該表格的表格名字為 Store_Information, 一共有三個欄位, 分別為 store_name , Sales , Data , 已經錄入了四筆數據所以有四個列位。
②關於數據類型
和其他的資料庫不同的是,sqlite 是無類型的。也就是當你創建一個表格時,無需對 每一個欄位要存儲的數據的類型進行聲明,當你在給表格增加數據條目時,sqlite 會自動找 到存入的數據的類型。
SQLite 允許忽略數據類型,但是,仍然建議在 Create Table 語句中指定數據類型, 因為數據類型有利於增強程序的可讀性。SQLite 支持常見的數據類型,如 VARCHAR、 NVARCHAR、TEXT、INTEGER、FLOAT、BOOLEAN、CLOB、BLOB、TIMESTAMP、 NUMERIC、VARYING、CHARACTER、NATl0NAI, VARYINGCHARACTER。這些數據類 型都是 SQL92 標准中規定的標准資料庫數據類型,想要有更近一步了解,請參看下錶。
SQL 資料庫數據類型詳解 數據類型 類型 描 述 bit 整型 bit 數據類型是整型,其值只能是 0、1 或空值。這種數據類型用於存儲只有兩 種可能值的數據,如 Yes 或 No、True 或 Fa lse 、On 或 Off int 整型 int 數據類型可以存儲從- 231(-2147483648)到 231 (2147483 647)之間的整數。存 儲到資料庫的幾乎所有數值型的數據都可以用這種數據類型。 這種數據類型在資料庫里佔用 4 個位元組 smallint 整型 smallint 數據類型可以存儲從- 215(-32768)到 215(32767)之間的整數。這種 數據類型對存儲一些常限定在特定范圍內的數值型數據非常有用。 這種數據類型在資料庫里 佔用 2 位元組空間 tinyint 整型 tinyint 數據類型能存儲從 0 到 255 之間的整數。 它在你只打算存儲有限數目 的數值時很有用。 這種數據類型在資料庫中佔用 1 個位元組 numeric 精確數值型 numeric 數據類型與 decimal 型相同 decimal 精確數值型 decimal 數據類型能用來存儲從-1038-1 到 1038-1 的固定精度和范圍 的數值型數據。使用這種數據類型時,必須指定范圍和精度。 范圍是小數點左右所能存儲

④ android 怎麼往資料庫裡面添加數據

一、引入
資料庫創建的問題解決了,接下來就該使用資料庫實現應用程序功能的時候了。基
本的操作包括創建、讀取、更新、刪除,即我們通常說的 CRUD(Create, Read, Update, Delete)。
在實現這些操作的時候,我們會使用到兩個比較重要的類 SQLiteDatabase 類和 Cursor 類。

二、創建表
1,execSQL(String sql):執行一條 sql 語句,且執行操作不能為 SELECT
因為它的返回值為 void,所以推薦使用 insert、update 方法等
2.,execSQL (String sql,Object[] bindArgs)
sql:執行一條 sql 語句
bindArgs:為 sql 語句中的?賦值

三、添加數據
1、execSQL(String sql)
2、使用對象的 insert 方法
ContentValues values = new ContentValues();
values.put(USERNAME, user.getUsername());
values.put(PASSWORD, user.getPassword());
db.insert(TABLE_NAME, null, values);
參數:
table:資料庫中的表名
nullColumnHack:指定默認插入欄位,為 null 時能插入數據
values:表示插入欄位所對應的值,使用 put 方法。

四、刪除數據
1、execSQL(String sql)
2、使用對象的 delete 方法
String whereClaues="_id=?";
String [] whereArgs={String.valueOf(id)};
//db.delete(TABLE_NAME, "_id="+id, null);
db.delete(TABLE_NAME, whereClaues, whereArgs);
參數
table:資料庫的表名
whereClause:where 子句,比如:_id=?
whereArgs:where 子句中?的值

五、修改數據
1、execSQL(String sql)
2、使用對象的 delete 方法
ContentValues values = new ContentValues();
values.put(USERNAME, user.getUsername());
values.put(PASSWORD, user.getPassword());
String whereClaues="_id=?";
String [] whereArgs={String.valueOf(user.getId())};
db.update(TABLE_NAME, values, whereClaues, whereArgs);
參數
table:資料庫的表名
values:代表要修改的值,修改方法還是 put(key,values)
whereClause:條件子句,比如 id=?,name=?
whereArgs:為 whereClause 中的?賦值,比如:new String[]{"1","張三"}

圖:

參考代碼:

程序內使用SQLite資料庫是通過SQLiteOpenHelper進行操作

1.自己寫個類繼承SQLiteOpenHelper,重寫以下3個方法

publicvoidonCreate(SQLiteDatabasedb)

{//創建資料庫時的操作,如建表}

publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion)

{

//版本更新的操作

}

2.通過SQLiteOpenHelper的getWritableDatabase()獲得一個SQLiteDatabase資料庫,以後的操作都是對SQLiteDatabase進行操作。

3.對得到的SQLiteDatabase對象進行增,改,刪,查等操作。

代碼

packagecx.myNote;

importandroid.content.ContentValues;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.database.Cursor;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.database.sqlite.SQLiteOpenHelper;

//DBOptionsforlogin

publicclassDBOptions{

privatestaticfinalStringDB_NAME="notes.db";

privatestaticfinalStringDB_CREATE="createtablelogininf(nametext,pwdtext)";

{

publicDBHelper(Contextcontext){

super(context,DB_NAME,null,1);

}

@Override

publicvoidonCreate(SQLiteDatabasedb){

//TODOAuto-generatedmethodstub

//建表

db.execSQL(DB_CREATE);

}

@Override

publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){

//TODOAuto-generatedmethodstub

db.execSQL("droptableifexistslogininf");

onCreate(db);

}

}

privateContextcontext;

privateSQLiteDatabasedb;

privateDBHelperdbHelper;

publicDBOptions(Contextcontext)

{

this.context=context;

dbHelper=newDBHelper(context);

db=dbHelper.getReadableDatabase();

}

//自己寫的方法,對資料庫進行操作

publicStringgetName()

{

Cursorcursor=db.rawQuery("selectnamefromlogininf",null);

cursor.moveToFirst();

returncursor.getString(0);

}

publicintchangePWD(StringoldP,Stringpwd)

{

ContentValuesvalues=newContentValues();

values.put("pwd",pwd);

returndb.update("logininf",values,"pwd="+oldP,null);

}

}


insert方法插入的一行記錄使用ContentValus存放,ContentValues類似於Map,它提供了put(String key, Xxx value)(其中key為數據列的列名)方法用於存入數據、getAsXxxx(String key)方法用於取出數據

⑤ 跪求android將Excel表格中的數據導入資料庫中的demo

我只能給您思路,DEMO我沒。。。

這個你可以網路下具體過程。

我來告訴你大致思路。

  1. 首先,根據excel表格創建對象。最好欄位都一樣,不然怕你自己整迷糊了。

  2. 讀取excel文件

  3. 寫入資料庫

    資料庫工具:你可以用GreenDAO3.0框架,15分鍾差不多能學會,很簡單方便使用。其他沒有什麼難度點了。

⑥ android 怎麼把資料庫表數據 寫入二維數組

這個例子是將資料庫中的數據存儲到集合中
List<ContactInfo> list = new ArrayList<ContactInfo>();//ContactInfo實體類
SQLiteDatabase db = dbOpentHlper.getReadableDatabase();
String sql = "select * from contactinfo";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String qq = cursor.getString(cursor.getColumnIndex("qq"));
String msn = cursor.getString(cursor.getColumnIndex("msn"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
list.add(new ContactInfo(id, name, qq, msn, phone));
}

⑦ Android資料庫操作表的兩種方法,通過sql 語句法和谷歌提供的api 方法進行比較情況如何

DirverManager類:是JDBC的管理層,作用於用戶和驅動之間。該類負責注冊和載入JDBC驅動。
Connection介面:代表與資料庫的鏈接,並擁有創建SQL語句的方法,以完成基本的SQL操作,同時為資料庫事務提供提交和回滾方法。如:上面的例子就是鏈接到了TestData資料庫。
Statement介面:用於執行不帶參數的簡單SQL語句。創建Statement實例對象後可以調用JDBC提供的3種執行SQL語句的方法:
(1)executeUpdate()方法,一般用於執行SQL的INSERT,DELETE,UPDATE語句
(2)executeQuery()方法,一般用於執行SQL的SELECT語句,因為 它的返回值是執行SQL語句後產生的一個ResultSet介面的實例(結果集)
(3)execute()方法,即一般它執行的SQL語句既有查詢又有更新值,約等於executeUpdate()和executeQuery()兩個方法的合輯。
PreparedStatement介面:它與Statement 的主要區別
(1)它包含的SQL語句是預編譯的,所以當多次執行一條SQL語句時用它會更快
(2)在設置參數是可以用「?」代替。如:
PreparedStatement pstmt=conn.preparedStatement(insert into test values(?,?));
pstmt.setString(1,'gg');
pstmt.setString(2,'123');
ResultSet介面:包含了Statement和PreparedStatement的executeQuery方法中SELECT的結果集。相當於用它來讀取資料庫里每列的值。
DatabaseMetaData介面:主要是用來得到資料庫的相關信息的。如:資料庫版本啊
ResultSetMetaData介面:主要是用來獲取資料庫中表的相關信息的。如:表的行數啊。,謝謝

⑧ android資料庫存儲,代碼創建資料庫、表,實現編輯框內輸入,點擊按鈕提交進行存入;從資料庫中提

常量類:ConstantUtil

java">publicclassConstantUtil{

//資料庫名稱
_NAME="user_manager.db";
//資料庫版本號
publicstaticfinalintDATABASE_VERSION=1;

//表名
publicstaticfinalStringTABLE_NAME="user_info";
//欄位名
publicstaticfinalStringUSER_ID="userId";
publicstaticfinalStringUSER_NAME="username";
publicstaticfinalStringUSER_PASSWORD="password";
publicstaticfinalStringUSER_ADDRESS="address";



}


自定義SQLiteOpenHelper:MySQLiteOpenHelper

{

//定義一個SQLiteDatabase對象,對表進行相應的操作
;

publicMySQLiteOpenHelper(Contextcontext){
super(context,ConstantUtil.DATABASE_NAME,null,
ConstantUtil.DATABASE_VERSION);
mDatabase=getWritableDatabase();
}

/*
*創建表
*/
@Override
publicvoidonCreate(SQLiteDatabasedb){
//TODOAuto-generatedmethodstub

//sql語句
Stringsql="createtable"+ConstantUtil.TABLE_NAME+"("
+ConstantUtil.USER_ID+"integerprimarykey,"
+ConstantUtil.USER_NAME+"textnotnull,"
+ConstantUtil.USER_PASSWORD+"textnotnull,"
+ConstantUtil.USER_ADDRESS+"textnotnull)";
db.execSQL(sql);
}

@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
//TODOAuto-generatedmethodstub
onCreate(db);
}

/**
*添加數據
*@paramcv
*@return
*/
publicbooleaninsertData(ContentValuescv){
returnmDatabase.insert(ConstantUtil.TABLE_NAME,null,cv)>0;
}

/**
*查詢所有數據
*@return
*/
publicList<Userinfo>queryData(){

List<Userinfo>userinfos=newArrayList<Userinfo>();
//從資料庫里查詢數據
Cursorcursor=mDatabase.query(ConstantUtil.TABLE_NAME,null,null,null,null,null,null);

if(cursor!=null){
//取出數據
while(cursor.moveToNext()){
Userinfouserinfo=newUserinfo();
userinfo.setUserId(cursor.getInt(0));
userinfo.setUsername(cursor.getString(1));
userinfo.setPassword(cursor.getString(2));
userinfo.setAddress(cursor.getString(3));
userinfos.add(userinfo);
}

}
returnuserinfos;

}

}

主Activity

{
//控制項
privateTextViewtxtName,txtPwd,txtAddress;
privateEditTextedtName,edtPwd,edtAddress;
privateListViewmListView;
//資料庫對象
;

privateUserinfoAdapteradapter;
/**.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

findView();
init();
}

privatevoidfindView(){
//TODOAuto-generatedmethodstub

edtName=(EditText)findViewById(R.id.id_edit1);
edtPwd=(EditText)findViewById(R.id.id_edit2);
edtAddress=(EditText)findViewById(R.id.id_edit3);
mListView=(ListView)findViewById(R.id.id_listview);

}

privatevoidinit(){
//TODOAuto-generatedmethodstub
mySQLiteOpenHelper=newMySQLiteOpenHelper(MainActivity.this);
}

publicvoidonAction(Viewv){

switch(v.getId()){
caseR.id.id_btn_add:
//添加數據
StringuserName=edtName.getText().toString();
StringuserPwd=edtPwd.getText().toString();
StringuserAdress=edtAddress.getText().toString();
//傳入參數
ContentValuescv=newContentValues();
//列名和值
cv.put(ConstantUtil.USER_NAME,userName);
cv.put(ConstantUtil.USER_PASSWORD,userPwd);
cv.put(ConstantUtil.USER_ADDRESS,userAdress);
//得到結果
booleanflag=mySQLiteOpenHelper.insertData(cv);

if(flag){

Toast.makeText(MainActivity.this,"添加記錄成功",Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(MainActivity.this,"添加記錄失敗",Toast.LENGTH_SHORT).show();
}

break;
caseR.id.id_btn_query:
//查詢數據
List<Userinfo>userinfos=mySQLiteOpenHelper.queryData();
if(adapter!=null){
adapter=null;
}
adapter=newUserinfoAdapter(userinfos);
mListView.setAdapter(adapter);

break;
default:
break;
}

}

//數據適配器
{

List<Userinfo>userinfos;
publicUserinfoAdapter(List<Userinfo>_userinfos){
this.userinfos=_userinfos;
}

@Override
publicintgetCount(){
//TODOAuto-generatedmethodstub
returnuserinfos.size();
}

@Override
publicObjectgetItem(intposition){
//TODOAuto-generatedmethodstub
returnuserinfos.get(position);
}

@Override
publiclonggetItemId(intposition){
//TODOAuto-generatedmethodstub
returnposition;
}

@Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
//TODOAuto-generatedmethodstub
if(convertView==null){
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.listview_item,null);

txtName=(TextView)convertView.findViewById(R.id.id_txt_name);
txtPwd=(TextView)convertView.findViewById(R.id.id_txt_pwd);
txtAddress=(TextView)convertView.findViewById(R.id.id_txt_address);

txtName.setText(userinfos.get(position).getUsername());
txtPwd.setText(userinfos.get(position).getPassword());
txtAddress.setText(userinfos.get(position).getAddress());
}


returnconvertView;
}

}
}


完整源碼下載地址(附資料庫文件查詢軟體+運行效果圖):

⑨ 如何往android中添加資料庫

一、新建外部SQLite資料庫
(1)下載並安裝 SQLite可視化管理工具(SQLite Expert Pro) v3.4.17 破解版
http://www.cr173.com/soft/36343.html
(2)將你手頭上的數據放到EXCEL表格中,保存為CSV格式的數據
(3)在此工具中按照你現有的數據格式新建資料庫和表,如資料庫為:contact.db,表為employee
(4)通過此工具菜單欄中Import/Export下的Import text file(CSV,TSC)功能,將你現有的CSV數據導入到你新建的數據表中(主要目的是省的一個一個的錄入了)
二、在eclipse中新建一個android app工程,並在新建的工程文件夾點右鍵new->folder,在res文件夾下新建raw文件夾(如果有就不用新建了)
三、用滑鼠將新建的SQLite資料庫文件contact.db拖動到新建工程的res下的raw文件下,出現提示,選擇
四、程序代碼
private static final String DATABASE_PATH = "/data/data/你的主程序包路徑(如:com.szair.contact)/databases";
private static final int DATABASE_VERSION = 0;
private static final String DATABASE_NAME = "contact.db";
private static String outFileName = DATABASE_PATH + "/" + DATABASE_NAME;
try {
buildDatabase();//見下
} catch (Exception e) {
e.printStackTrace();
}
//SQLiteDatabase對象
SQLiteDatabase db=SQLiteDatabase.openDatabase(outFileName, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
String t="SELECT 欄位名1,欄位名2 FROM employee WHERE **** ORDER BY ***";
Cursor c =db.rawQuery(t, null);
if(c.moveToFirst()){
for(int i=0;i
{
String zian1=c.getString(0);//欄位1的數據
String zian2=c.getString(1);//欄位1的數據
}
}
------------------------------------------------
//前面用到的buildDatabase方法
private void buildDatabase() throws Exception{
InputStream myInput = getResources().openRawResource(R.raw.sz_contact);
File file = new File(outFileName);
File dir = new File(DATABASE_PATH);
if (!dir.exists()) {
if (!dir.mkdir()) {
throw new Exception("創建失敗");
}
}

if (!file.exists()) {
try {
OutputStream myOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.close();
myInput.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}
五、程序發布
按照以上方式,可以將外部建的SQLite資料庫成功的發布出來

熱點內容
ftp上傳文件shell 發布:2024-05-22 08:18:05 瀏覽:876
安卓airpords降噪怎麼弄 發布:2024-05-22 08:18:01 瀏覽:492
se腳本 發布:2024-05-22 08:09:07 瀏覽:964
怎麼刪除linux 發布:2024-05-22 08:04:50 瀏覽:149
go和python 發布:2024-05-22 07:58:26 瀏覽:237
at24c02存儲晶元的參考文獻 發布:2024-05-22 07:28:26 瀏覽:727
微信apiphp 發布:2024-05-22 07:26:49 瀏覽:148
編譯kernel的流程 發布:2024-05-22 07:26:47 瀏覽:939
u盤頭戴式耳機怎麼連接安卓手機 發布:2024-05-22 07:25:30 瀏覽:603
pc換圖腳本 發布:2024-05-22 07:24:00 瀏覽:111