新聞中心
SQLite是一種輕量級(jí)的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它適用于小型的應(yīng)用程序和嵌入式設(shè)備。雖然SQLite沒(méi)有提供諸如MySQL或Oracle等更大型的數(shù)據(jù)庫(kù)管理系統(tǒng)中的高級(jí)功能,但是它可以輕松地嵌入到應(yīng)用程序中,并且查詢速度非??臁T贏ndroid應(yīng)用程序開(kāi)發(fā)中,SQLite作為默認(rèn)的嵌入式數(shù)據(jù)庫(kù)經(jīng)常被使用。下面,我們將介紹如何。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供橫縣網(wǎng)站建設(shè)、橫縣做網(wǎng)站、橫縣網(wǎng)站設(shè)計(jì)、橫縣網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、橫縣企業(yè)網(wǎng)站模板建站服務(wù),十多年橫縣做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
之一步:建立SQLite數(shù)據(jù)庫(kù)
需要在應(yīng)用程序中建立SQLite數(shù)據(jù)庫(kù)。您可以使用Android開(kāi)發(fā)工具(ADT)的SQLite Database Browser來(lái)創(chuàng)建數(shù)據(jù)庫(kù)。在ADB shell的命令行界面中,輸入以下命令來(lái)創(chuàng)建數(shù)據(jù)庫(kù):
sqlite3
代表您想要?jiǎng)?chuàng)建的數(shù)據(jù)庫(kù)的名稱。執(zhí)行以上命令后,ADT將在你的應(yīng)用程序中自動(dòng)生成一個(gè)SQLite數(shù)據(jù)庫(kù)。您可以通過(guò)運(yùn)行以下命令在ADB shell 中查看您的數(shù)據(jù)庫(kù):
.sqlite_master中
這個(gè)命令將列出您在數(shù)據(jù)庫(kù)中創(chuàng)建的所有表。
第二步:創(chuàng)建表
接下來(lái),您需要為您的應(yīng)用程序創(chuàng)建表。SQLite使用SQL查詢語(yǔ)言來(lái)建立表格。例如,以下是用于建立一個(gè)名為“student”的表的查詢語(yǔ)句:
create table student ( id integer primary key autoincrement, name text not null, age integer not null );
在此查詢語(yǔ)句中,您可以看到表格包含3個(gè)字段:id(整數(shù)類型,主鍵和自動(dòng)增量),name(文本類型,不能為空),age(整數(shù)類型,不能為空)。當(dāng)您運(yùn)行這個(gè)查詢語(yǔ)句時(shí),SQLite會(huì)在您的數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)新表“students”。
第三步:插入數(shù)據(jù)
在您的表中建立后,就可以開(kāi)始向其中添加數(shù)據(jù)。使用INSERT查詢語(yǔ)句可以將數(shù)據(jù)寫(xiě)入表中。例如,以下是向“student”表中添加一行數(shù)據(jù)的查詢語(yǔ)句:
insert into student (name, age) values (‘Tom’, 18);
在此查詢語(yǔ)句中,您可以看到我們將“Tom”和“18”作為數(shù)據(jù)對(duì)填入到“student”表格的“name”和“age”字段中。同樣,您可以使用相同的INSERT查詢語(yǔ)句向表格添加更多的數(shù)據(jù)。
第四步:查詢數(shù)據(jù)
一旦您已將數(shù)據(jù)插入到表中,就可以開(kāi)始從表格中檢索數(shù)據(jù)。使用SELECT查詢語(yǔ)句可以從表中檢索數(shù)據(jù)。例如,以下是查詢“student”表中所有數(shù)據(jù)的查詢語(yǔ)句:
select * from student;
在此查詢語(yǔ)句中,我們使用“*”通配符來(lái)檢索表格中所有數(shù)據(jù)。如果您只需要檢索特定行或列的數(shù)據(jù),則可以使用更復(fù)雜的查詢語(yǔ)句。
第五步:在ListView中顯示數(shù)據(jù)
現(xiàn)在您已經(jīng)成功地使用SQLite在您的應(yīng)用程序中創(chuàng)建了數(shù)據(jù)庫(kù),并向其中添加數(shù)據(jù)。下一步是以一種美觀友好的方式在您的應(yīng)用程序中顯示這些數(shù)據(jù)。您可以使用Android的ListView控件來(lái)顯示SQLite數(shù)據(jù)庫(kù)中的數(shù)據(jù)。使用CursorAdapter可以使ListView控件與SQLite數(shù)據(jù)庫(kù)進(jìn)行交互,并顯示數(shù)據(jù)庫(kù)中的數(shù)據(jù)。例如,以下是ListView控件顯示從“student”表中檢索的數(shù)據(jù)的代碼:
Cursor cursor = db.query(“student”, null, null, null, null, null, null);
String[] from = { “name”, “age” }; int[] to = { R.id.name, R.id.age };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview_item, cursor, from, to, 0);
listView.setAdapter(adapter);
在此代碼中,我們首先使用Cursor對(duì)象從“student”表中檢索所有數(shù)據(jù)。然后,我們使用from和to數(shù)組來(lái)指定應(yīng)在ListView控件中顯示哪些字段。我們使用SimpleCursorAdapter將Cursor對(duì)象與ListView控件進(jìn)行連接。
結(jié)論
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián),建站經(jīng)驗(yàn)豐富以策略為先導(dǎo)10多年以來(lái)專注數(shù)字化網(wǎng)站建設(shè),提供企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站制作,設(shè)計(jì)師量身打造品牌風(fēng)格,熱線:028-86922220如何從sqlite數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)并顯示在listview中
在登錄頁(yè)面后,我想在listview中把Apple顯示成A,Boy顯示成B等塵輪罩等,直到F。但派鬧是在程序中當(dāng)我完全登錄后,只有登錄表成功創(chuàng)建,主菜單還是沒(méi)有創(chuàng)建。
我想桐友在test database中創(chuàng)建主菜單,然后我想從主菜單表(mainmenu table)中獲取數(shù)據(jù)再顯示在listview中。
我使用了下面的代碼:
if(username.length()>0&&password.length()>0)
{
SQLiteAdapter db=new SQLiteAdapter(Main.this);
db.openToWrite();
if(db.Login(username,password))
{
System.out.println(“goutham”);
Intent intent=new Intent(getApplicationContext(),ExampleActivity.class);
startActivity(intent);
}
SQLiteAdapter.java
}
public Cursor queueAll() {
String columns = new String { KEY_ID, KEY_CONTENT };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
null, null, null, null);
return cursor;
}
private static class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public long AddUser(String username, String password) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, username);
initialValues.put(KEY_PASSWORD, password);
return sqLiteDatabase.insert(DATABASE_TABLE, null, initialValues);
}
public boolean Login(String username, String password) {
// TODO Auto-generated method stub
Cursor mCursor = sqLiteDatabase.rawQuery(“SELECT * FROM “
+ DATABASE_TABLE + ” WHERE username=? AND password=?”,
new String { username, password });
if (mCursor != null) {
if (mCursor.getCount() > 0) {
return true;
}
}
return false;
}
}
ExampleActivity.java
public class ExampleActivity extends Activity {
private SQLiteAdapter mySQLiteAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listContent = (ListView) findViewById(R.id.contentlist);
/*
* Create/Open a SQLite database and fill with dummy content and close
* it
*/
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
// mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert(“A for Apply”);
mySQLiteAdapter.insert(“B for Boy”);
mySQLiteAdapter.insert(“C for Cat”);
mySQLiteAdapter.insert(“D for Dog”);
mySQLiteAdapter.insert(“E for Egg”);
mySQLiteAdapter.insert(“F for Fish”);
mySQLiteAdapter.close();
/*
* Open the same SQLite database and read all it’s content.
*/
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String from = new String { SQLiteAdapter.KEY_CONTENT };
int to = new int { R.id.text };
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
運(yùn)行程序后,登錄表在數(shù)據(jù)庫(kù)中創(chuàng)建了,但是主菜單表沒(méi)有創(chuàng)建。運(yùn)行程序后,顯示一個(gè)錯(cuò)誤:
sqlite returned code=1 no such a table in MY_TABLE
通過(guò)互聯(lián)網(wǎng)整理獲得以下解決方法:
=================1樓=====================
Database class:
public String getData1() throws SQLException{
// TODO Auto-generated method stub
String columns1 = new String { KEY_DATE };
Cursor c1 = ourDatabase.query(DATABASE_MARKSTABLE, columns1, null, null, null,
null, KEY_ENDINGTIME+” DESC”, ” 30″);
String result1 = “”;
int isName = c1.getColumnIndex(KEY_DATE);
for (c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()) {
result1 = result1 + c1.getString(isName)
+ ” ” + “\n”;
}
c1.close();
return result1;
}
代碼:前蠢
if(username.length()>0&&password.length()>改昌0)
{
SQLiteAdapter db=new SQLiteAdapter(Main.this);
db.openToWrite();
if(db.Login(username,password))
{
System.out.println(“核悔扒goutham”);
Intent intent=new Intent(getApplicationContext(),ExampleActivity.class);
startActivity(intent);
}
關(guān)于獲取listview數(shù)據(jù)庫(kù)的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都網(wǎng)站建設(shè)選創(chuàng)新互聯(lián)(?:028-86922220),專業(yè)從事成都網(wǎng)站制作設(shè)計(jì),高端小程序APP定制開(kāi)發(fā),成都網(wǎng)絡(luò)營(yíng)銷推廣等一站式服務(wù)。
分享名稱:使用SQLite幫助您獲取listview數(shù)據(jù)庫(kù)(獲取listview數(shù)據(jù)庫(kù))
瀏覽路徑:http://www.dlmjj.cn/article/dhpdcgd.html


咨詢
建站咨詢
