如何在Linux下用CC++语言 *** 作数据库sqlite3

如何在Linux下用CC++语言 *** 作数据库sqlite3,第1张

sqlite3是跨平台的 只要能读写文件就能使用sqlite3

这种高词频出现的第三方框架,各种例子一大堆,我以前领导经常对新手说一句话,一个新手能遇到的问题,那前面已经有无数个人遇到过了,所以资料肯定一大堆。如果你遇到了连资料都搜不到的问题,那你已经是高手了。

搜索 “sqlite3 例子”

sqlite是一个嵌入式的SQL数据库引擎,只用一个文件就ok,小巧方便,所以是一个非常不错的嵌入式数据库,在Mozilla Firefox使用SQLite作为数据库。

例如:存放cookies的文件就是一个SQLite的数据库文件,一般存在于 ~/mozilla/firefox/cojs83dhdefault目录下,那么如何进行查看呢?

可使用vim打开文件,查看到使用的格式是sqlite3,而在GNU Linux下有一个命令可以对其进行查看:sqlite3,以下就是具体的使用步骤:

0、使用命令对此数据库进行打开:

$ sqlite3 cookiessqlite

进行入sqlite3命令行模式;

1、查看此文件中的数据库的名称:

sqlite> databases

seq name file

--- --------------- ----------------------------------------------------------

0 main /home/ufo/mozilla/firefox/cojs83dhdefault/cookiessqlite

2、再查看此文件中已经存在数据表:

sqlite> tables

moz_cookies

可看到此文件中包含一个表,名为moz_cookies;

3、查看此表中的字段名称:

sqlite> schema moz_cookies

CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, baseDomain TEXT, appId INTEGER DEFAULT 0, inBrowserElement INTEGER DEFAULT 0, name TEXT, value TEXT, host TEXT, path TEXT, expiry INTEGER, lastAccessed INTEGER, creationTime INTEGER, isSecure INTEGER, is>

在登录页面后,我想在listview中把Apple显示成A,Boy显示成B等等,直到F。但是在程序中当我完全登录后,只有登录表成功创建,主菜单还是没有创建。

我想在test database中创建主菜单,然后我想从主菜单表(mainmenu table)中获取数据再显示在listview中。

我使用了下面的代码:

if(usernamelength()>0&&passwordlength()>0)

{

SQLiteAdapter db=new SQLiteAdapter(Mainthis);

dbopenToWrite();

if(dbLogin(username,password))

{

Systemoutprintln("goutham");

Intent intent=new Intent(getApplicationContext(),ExampleActivityclass);

startActivity(intent);

}

SQLiteAdapterjava

}

public Cursor queueAll() {

String[] columns = new String[] { KEY_ID, KEY_CONTENT };

Cursor cursor = sqLiteDatabasequery(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

dbexecSQL(SCRIPT_CREATE_DATABASE);

dbexecSQL(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();

initialValuesput(KEY_USERNAME, username);

initialValuesput(KEY_PASSWORD, password);

return sqLiteDatabaseinsert(DATABASE_TABLE, null, initialValues);

}

public boolean Login(String username, String password) {

// TODO Auto-generated method stub

Cursor mCursor = sqLiteDatabaserawQuery("SELECT FROM "

+ DATABASE_TABLE + " WHERE username= AND password=",

new String[] { username, password });

if (mCursor != null) {

if (mCursorgetCount() > 0) {

return true;

}

}

return false;

}

}

ExampleActivityjava

public class ExampleActivity extends Activity {

private SQLiteAdapter mySQLiteAdapter;

/ Called when the activity is first created /

@Override

public void onCreate(Bundle savedInstanceState) {

superonCreate(savedInstanceState);

setContentView(Rlayoutmain);

ListView listContent = (ListView) findViewById(Ridcontentlist);

/

Create/Open a SQLite database and fill with dummy content and close

it

/

mySQLiteAdapter = new SQLiteAdapter(this);

mySQLiteAdapteropenToWrite();

// mySQLiteAdapterdeleteAll();

mySQLiteAdapterinsert("A for Apply");

mySQLiteAdapterinsert("B for Boy");

mySQLiteAdapterinsert("C for Cat");

mySQLiteAdapterinsert("D for Dog");

mySQLiteAdapterinsert("E for Egg");

mySQLiteAdapterinsert("F for Fish");

mySQLiteAdapterclose();

/

Open the same SQLite database and read all it's content

/

mySQLiteAdapter = new SQLiteAdapter(this);

mySQLiteAdapteropenToRead();

Cursor cursor = mySQLiteAdapterqueueAll();

startManagingCursor(cursor);

String[] from = new String[] { SQLiteAdapterKEY_CONTENT };

int[] to = new int[] { Ridtext };

SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,

Rlayoutrow, cursor, from, to);

listContentsetAdapter(cursorAdapter);

mySQLiteAdapterclose();

}

}

运行程序后,登录表在数据库中创建了,但是主菜单表没有创建。运行程序后,显示一个错误:

sqlite returned code=1 no such a table in MY_TABLE

通过互联网整理获得以下解决方法:

=================1楼=====================

Database class:

public String getData1() throws SQLException{

// TODO Auto-generated method stub

String[] columns1 = new String[] { KEY_DATE };

Cursor c1 = ourDatabasequery(DATABASE_MARKSTABLE, columns1, null, null, null,

null, KEY_ENDINGTIME+" DESC", " 30");

String result1 = "";

int isName = c1getColumnIndex(KEY_DATE);

for (c1moveToFirst(); !c1isAfterLast(); c1moveToNext()) {

result1 = result1 + c1getString(isName)

+ " " + "\n";

}

c1close();

return result1;

}

以上就是关于如何在Linux下用C/C++语言 *** 作数据库sqlite3全部的内容,包括:如何在Linux下用C/C++语言 *** 作数据库sqlite3、如何读取 sqlite3 sqlite3、如何从sqlite数据库中获取数据并显示在listview中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9378291.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存