string ConnectStr = "Data Source=datadb";
string SearchComm = "select from abgc";
SQLiteConnection selectConnection = new SQLiteConnection(ConnectStr);
DataSet dataSet = new DataSet();
if (selectConnectionState != SystemDataConnectionStateOpen) {
selectConnectionOpen();
SQLiteCommand cmd = selectConnectionCreateCommand();
cmdCommandText = SearchComm;
SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd);
adapterFill(dataSet);
selectConnectionClose();
}
dataGridView1DataSource = dataSetTables[0];
用法:
string为字符串;
start为起始位置;字符串的第一个字符的位置为1,不是从0开始计算
length为长度。
2、SQL语句
select substr([dirpro],2,32) from table1//查询parentid
select from table1 where substr([dirpro],2,32)="NULL"
登录后复制
五、例子
假设表table1
存在以下数据
mark
1000
1000
0001
0002
select mark from table1 where substr(mark,1,2)='00'
结果集如下:
mark
在登录页面后,我想在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;
}
SqLiteDatabase查询并获取里面的数值的实现,案例(查询下图表格里面不同的name对应的value值)代码如下:
public int cgQueryItemValue(String name){
Loge(TAG, "hongyan:cgQueryItemValue name=" +name);
try {
Cursor c =mSqLiteDatabasequery(ConstENG_STRING2INT_TABLE,
new String[] {
ConstENG_STRING2INT_NAME,ConstENG_STRING2INT_VALUE
},
ConstENG_STRING2INT_NAME + "= \'" + name + "\'", null, null, null, null);
//上述的Cursor c 获取到的是指定name对应的一行(只包括指定列)也就是下图这样的:if (c !=null) {
cmoveToFirst();//必须写,否则读不到数据,将Index移动到第一位上
int valueIndex=cgetColumnIndexOrThrow(ConstENG_STRING2INT_VALUE);
//如上图,valueIndex= 1;
int value =cgetInt(valueIndex);
//注意value值是什么类型用合适的get,如果是String就得用getString!!否则会有异常
cclose();
return value;
}
} catch (Exception e) {
return 0;
}
return 0;
}
protected int cgQueryAutoTestFailCount()
{
int failCount = 0;
int result =0;
for(String itemName:ConstCG_DEFAULT_AUTO_TEST_ITEMS_NAME)
{
result =mEngSqlitecgQueryItemValue(itemName);
//Logd(TAG, "hongyan: cgQueryAutoTestFailCount item test result = " + result);
if(result== 0)
{
failCount++;
}
}
Logd(TAG, "hongyan: cgQueryAutoTestFailCount failCount = " + failCount);
return failCount;
}
列出数据库的全部表名
select name from sysobjects where type='u'
select count() from sysobjects where id = object_id('数据库名Owner表名')OBJECT_ID返回数据库对象标识号。
语法OBJECT_ID ( 'object' )参数'object'要使用的对象。object 的数据类型为 char 或 nchar。如果 object 的数据类型是 char,那么
隐性将其转换成 nchar。
返回类型int注释当该参数对系统函数可选时,则系统采用当前数据库、主机、服务器用户或数据库用户。内
置函数后面必须跟圆括号。
如果指定一个临时表名,则必须在临时表名前面加上数据库名,例如:
SELECT OBJECT_ID('tempdb#mytemptable')
系统函数可以在选择列表、WHERE 子句和任何允许使用表达式的地方使用。有关更多信
息,请参见表达式和 WHERE。
示例下面的示例为 pubs 数据库中的 authors 表返回对象 ID。
USE masterSELECT OBJECT_ID('pubsauthors')
下面是结果集:
以上就是关于求解C#,将SQLite数据库data.db中的表abgc查询结果集显示到窗体的DataGridView控件中全部的内容,包括:求解C#,将SQLite数据库data.db中的表abgc查询结果集显示到窗体的DataGridView控件中、sqlite截取空格之后、如何从sqlite数据库中获取数据并显示在listview中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)