手机玩游戏出现sqlite 工具ExException

我遇到的这个错误是由sqlite查询语句产生的,我查看数据库文件,发现要查找的courseName是存在数据库中的,clCourseName代表的列明也是存在的,这两个都无误,所以不知道原因在哪里。
后来从网上找到原因:clCourseName代表的列是字符串类型的,查询语句中的字符串要用引号引上,courseName是字符串类型的,但是在代码中拼接成的查询查询语句里是没有引号的,所以报了错。
解决办法就是:在查询语句中加上引号。
原来的代码:
Cursor mCursor = db.query(true, tbnCourses,
new String[]{clCourseBeginweek,clCourseCredit,clCourseEndweek,clCourseLecturer,clCourseName},
clCourseName+"="+courseName, null, null, null, null, null);
修改后的代码:
Cursor mCursor = db.query(true, tbnCourses,
new String[]{clCourseBeginweek,clCourseCredit,clCourseEndweek,clCourseLecturer,clCourseName},
clCourseName+"='"+courseName+"'", null, null, null, null, null);
Views(...) Comments()模拟机运行的好好地,真机测试,sqlite数据库就出现有关问题
模拟机运行的好好地,真机测试,sqlite数据库就出现问题 09-.424:E/AndroidRuntime(11758):java.lang.RuntimeException:UnabletostartactivityComponentInfo{qiu.cn.water/qiu.cn.water.sensor.WaterLevelSensor}:android.database.sqlite.SQLiteEx
模拟机运行的好好地,真机测试,sqlite数据库就出现问题09-21&09:01:04.424:&E/AndroidRuntime(11758):&java.lang.RuntimeException:&Unable&to&start&activity&ComponentInfo{qiu.cn.water/qiu.cn.water.sensor.WaterLevelSensor}:&android.database.sqlite.SQLiteException:&no&such&table:&warninfo&(code&1):&,&while&compiling:&SELECT&*&FROM&warninfo&WHERE&location=?&and&sendDate=?------解决思路----------------------没有这个表,也就是说你的数据库表没有创建。
把真机上面的APP删除了再重新调试就行。
稳妥一些就把APP设置里面的数据也清除了。
你最喜欢的c# - SQLite.Net SQLiteException Constraint insert - Stack Overflow
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
I'm using SQLite.Net within a Xamarin Android application. I have an entity which looks like this:
[DataEntity]
public class AuditEntity
[PrimaryKey]
public Guid EntryId { }
public string Action { }
public string GeoLocation { }
public DateTime Time { }
I have a generic insert method which looks like this:
public int Insert&T&(T obj) where T : class
var result = 0;
result = Connection.Insert(obj);
catch (SQLiteException ex)
Mvx.Trace(MvxTraceLevel.Error, $"Exception while inserting into database:\r\n{ex.ToLongString()}");
I pass the following object into this method:
var auditEntry = new AuditEntity
EntryId = Guid.NewGuid(),
Action = uri.ToString(),
GeoLocation = geoLocation,
Time = DateTime.Now
Occasionally a SQLite exception is thrown with a message of "Constraint", which I understand is a violation of the primary key.
The EntryId column in the (automatically created) table is of type varchar(36).
Why would the insert sometimes throw the exception?
If I use a non-generic method, that does the insert via a parameterised command, I do not see the exception, but I'd rather use the generic method.
In above code 'EntryId' is set as a auto incremented column, So you don't need to insert the 'EntryId' value again and you can set datatype of 'EntryId' is integer.
If you want guid type of id, You can set EntryId = Guid.NewGuid().Tostring() in your entity Then every new value inserted in your table automatically create the entryId.(I have not much reputation to comment your question, so I have ping in form of answer.)
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledsqlite操作类
编辑:www.fx114.net
本篇文章主要介绍了"sqlite操作类",主要涉及到sqlite操作类方面的内容,对于sqlite操作类感兴趣的同学可以参考一下。
&&&& public abstract class SQLiteHelper&&& {&&&&&&& public static string connectionString = System.Web.HttpContext.Current.Server.MapPath(&~/DOWhat.s3db&);&&&&&&& #region 准备一个命令&&&&&&& /// &summary&&&&&&&& /// &&&&&&& /// &/summary&&&&&&&& /// &param name=&cmd&&SQLiteCommand&/param&&&&&&&& /// &param name=&conn&&数据库连接字符串&/param&&&&&&&& /// &param name=&trans&&事务&/param&&&&&&&& /// &param name=&cmdText&&sql语句&/param&&&&&&&& /// &param name=&cmdParms&&sql参数&/param&&&&&&&& private static void PrepareCommand(SQLiteCommand cmd,SQLiteConnection conn,SQLiteTransaction trans, string cmdText,SQLiteParameter[] cmdParms)&&&&&&& {&&&&&&&&&&& if (conn.State != ConnectionState.Open)&&&&&&&&&&& {&&&&&&&&&&&&&&& SQLiteConnectionStringBuilder connsb = new SQLiteConnectionStringBuilder();&&&&&&&&&&&&&&& connsb.DataSource = connectionS&&&&&&&&&&&&&&& conn.ConnectionString = connsb.ToString();&&&&&&&&&&&&&&& conn.Open();&&&&&&&&&&& }&&&&&&&&&&& cmd.Connection =&&&&&&&&&&& cmd.CommandText = cmdT&&&&&&&&&&& if (trans != null)&&&&&&&&&&& {&&&&&&&&&&&&&&& cmd.Transaction =&&&&&&&&&&& }&& &&&&&&&&&&& cmd.CommandType = CommandType.T//cmdT&&&&&&&&&&& if (cmdParms != null)&&&&&&&&&&& {&&&&&&&&&&&&&&& foreach (SQLiteParameter parameter in cmdParms)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&&&&&&&&&&&&&&&&&&&&&&&& (parameter.Value == null))&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& parameter.Value = DBNull.V&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& cmd.Parameters.Add(parameter);&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&& }&&&&&&& #endregion&&&&&&& #region 执行一条计算查询结果语句,返回查询结果&&&&&&& /// &summary&&&&&&&& /// 执行一条计算查询结果语句,返回查询结果(object)。&&&&&&& /// &/summary&&&&&&&& /// &param name=&SQLString&&计算查询结果语句&/param&&&&&&&& /// &returns&查询结果(object)&/returns&&&&&&&& public static object GetSingle(string SQLString)&&&&&&& {&&&&&&&&&&& using (SQLiteConnection connection = new SQLiteConnection(connectionString))&&&&&&&&&&& {&&&&&&&&&&&&&&& using (SQLiteCommand cmd = new SQLiteCommand())&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& try&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& PrepareCommand(cmd, connection, null, SQLString, null);&&&&&&&&&&&&&&&&&&&&&&& object obj = cmd.ExecuteScalar();&&&&&&&&&&&&&&&&&&&&&&& cmd.Parameters.Clear();&&&&&&&&&&&&&&&&&&&&&&& if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&& else&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& catch (System.Data.SQLite.SQLiteException e)&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&& }&&&&&&& /// &summary&&&&&&&& /// 执行一条计算查询结果语句,返回查询结果(object)。&&&&&&& /// &/summary&&&&&&&& /// &param name=&SQLString&&计算查询结果语句&/param&&&&&&&& /// &returns&查询结果(object)&/returns&&&&&&&& public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms)&&&&&&& {&&&&&&&&&&& using (SQLiteConnection connection = new SQLiteConnection(connectionString))&&&&&&&&&&& {&&&&&&&&&&&&&&& using (SQLiteCommand cmd = new SQLiteCommand())&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& try&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& PrepareCommand(cmd, connection, null, SQLString, cmdParms);&&&&&&&&&&&&&&&&&&&&&&& object obj = cmd.ExecuteScalar();&&&&&&&&&&&&&&&&&&&&&&& cmd.Parameters.Clear();&&&&&&&&&&&&&&&&&&&&&&& if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&& else&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& catch (System.Data.SQLite.SQLiteException e)&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&& }&&&&&&& #endregion&&&&&&& #region 执行一条SQL selet update delete返回是否成功的结果&&&&&&& /// &summary&&&&&&&& /// 执行一条SQL selet update delete返回是否成功的结果&&&&&&& /// &/summary&&&&&&&& /// &param name=&SQLString&&SQL语句&/param&&&&&&&& /// &returns&操作是否成功(bool)&/returns&&&&&&&& public static bool SQLExecute(string SQLString)&&&&&&& {&&&&&&&&&&& using (SQLiteConnection connection = new SQLiteConnection(connectionString))&&&&&&&&&&& {&&&&&&&&&&&&&&& using (SQLiteCommand cmd = new SQLiteCommand())&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& try&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& PrepareCommand(cmd, connection, null, SQLString, null);&&&&&&&&&&&&&&&&&&&&&&& int i = cmd.ExecuteNonQuery();&&&&&&&&&&&&&&&&&&&&&&& cmd.Parameters.Clear();&&&&&&&&&&&&&&&&&&&&&&& if (1==i)&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&& else&&&&&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&& catch (System.Data.SQLite.SQLiteException e)&&&&&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&& }&&&&&&& #endregion&&&&&&& #region 返回一个数据集&&&&&&& public static DataSet GetDS(string SQL)&&&&&&& {&&&&&&&&&&& using (SQLiteConnection connection =new SQLiteConnection(connectionString))&&&&&&&&&&& {&&&&&&&&&&&&&&& DataSet ds = new DataSet();&&&&&&&&&&&&&&& try&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& connection.Open();&&&&&&&&&&&&&&&&&&& SQLiteDataAdapter cmd = new SQLiteDataAdapter(SQL, connection);&&&&&&&&&&&&&&&&&&& cmd.Fill(ds, &ds&);&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& catch (System.Data.SQLite.SQLiteException ex)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& throw new Exception(ex.Message);&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& &&&&&&&&&&& }&&&&&&& }&&&&&&& #endregion&&& }
版权声明:本文为博主原创文章,未经博主允许不得转载。
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:SQLite问题。 。 急 。 高手进
编辑:www.fx114.net
本篇文章主要介绍了"SQLite问题。 。 急 。 高手进
40qq]",主要涉及到SQLite问题。 。 急 。 高手进
40qq]方面的内容,对于SQLite问题。 。 急 。 高手进
40qq]感兴趣的同学可以参考一下。
需求:&从一个数据库的某个表中获取数据往另外一个数据库的一个表中插.
因为数据量有点大&,&所以加载sql语句逐条insert不靠谱。&&太耗时&。&&找了半天找了个attach
但是不怎明白。&&请高手教一下。&。&我用的sqlite&developer&,&还不会用命令行。
高手支个招。&。&小弟万谢..............不要沉呀.&.&..&&sqllite是轻量级的数据库,没有这种高可用性的功能。
期待有经验的兄弟来解答下吧。帮助文档有没有?以前写的一个不知道能不能做参考
import&java.util.D
import&java.util.HashS
import&java.util.S
import&android.content.C
import&android.database.C
import&android.database.sqlite.SQLiteD
import&android.database.sqlite.SQLiteE
import&android.database.sqlite.SQLiteOpenH
import&android.database.sqlite.SQLiteDatabase.CursorF
public&class&DBAdapter&{
private&static&final&String&DB&=&"Sample.db";
private&static&final&String&DB_CREATE_EXPEND&=&"create&table&Expend(ID&integer&primary&key&autoincrement,Type&text,ExpendTime&datatime,Amount&real&not&null,Place&text)";
private&static&final&String&DB_CREATE_INCOME&=&"create&table&Income(ID&integer&primary&key&autoincrement,Type&text,IncomeTime&datatime,Amount&real&not&null)";
private&Context&
private&SQLiteDatabase&
private&DBHelper&dbH
public&DBAdapter(Context&context)&{
this.context&=&
dbHelper&=&new&DBHelper(context,&DB,&null,&1);
//&createTable();
//&数据库操作
public&void&open()&{
db&=&dbHelper.getWritableDatabase();
}&catch&(SQLiteException&ex)&{
db&=&dbHelper.getReadableDatabase();
public&void&close()&{
db.close();
public&void&insertExpend(Record&newExpend)&{
db.execSQL("insert&into&Expend(Type,ExpendTime,Amount,Place)values('"
+&newExpend.Type&+&"','"&+&newExpend.Time.getTime()&+&"',"
+&newExpend.Amount&+&",'"&+&newExpend.Place&+&"')");
private&Set&Record&&searchExpend(String&selection)&{
Set&Record&&records&=&new&HashSet&Record&();
Cursor&cursor&=&db.query("Expend",&new&String[]&{&"ID",&"Type",
"ExpendTime",&"Amount",&"Place"&},&selection,&null,&null,&null,
int&Type&=&cursor.getColumnIndex("Type");
int&ExpendTime&=&cursor.getColumnIndex("ExpendTime");
int&Amount&=&cursor.getColumnIndex("Amount");
int&Place&=&cursor.getColumnIndex("Place");
for&(cursor.moveToFirst();&!(cursor.isAfterLast());&cursor.moveToNext())&{
Record&newRecord&=&new&Record(cursor.getString(Type),&new&Date(
new&Long(cursor.getString(ExpendTime))),&new&Double(cursor
.getString(Amount)),&cursor.getString(Place));
records.add(newRecord);
public&class&DBHelper&extends&SQLiteOpenHelper&{
public&DBHelper(Context&context,&String&name,&CursorFactory&factory,
int&version)&{
super(context,&name,&factory,&version);
public&void&onCreate(SQLiteDatabase&db)&{
db.execSQL(DB_CREATE_EXPEND);
db.execSQL(DB_CREATE_INCOME);
public&void&onUpgrade(SQLiteDatabase&db,&int&oldVersion,&int&newVersion)&{
//&db.execSQL("drop&table&if&exists&User");
//&onCreate(db);
}引用&3&楼&chenzhp&的回复:帮助文档有没有?
有个attach&语句&.&可具体不知道怎么用.&&sqlite能不能实现???
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:

我要回帖

更多关于 sqlite expert 的文章

 

随机推荐