问题描述
我想学习的SQLite,试图基本的东西,并(如预期),它并没有真正发挥作用。
我试图创建另一个DB(不同的文件),但错误是一样的。
03-05 17:20:18.989:D / myLogs(1323):---插入mytable中:---
03-05 17:20:18.999:E / SQLiteLog(1323):(1)表mytable有没有指定的列日
03-05 17:20:19.199:E / SQLiteDatabase(1323):错误插入时间= ASD日期= ASD葡萄糖= ADF
03-05 17:20:19.199:E / SQLiteDatabase(1323):android.database.sqlite.SQLiteException:表mytable有没有指定的列日(code 1),在编译:INSERT INTO MYTABLE(时间,日期葡萄糖)VALUES(?,?,?)
这里是LogCat中输出:(不能添加图像,得给你一个链接)
基本上,它说,不存在一个名为日期
列下面是code:
包com.example.prototype2;进口android.os.Bundle;
进口android.app.Activity;
进口android.content.ContentValues;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.support.v4.app.NavUtils; 公共类AddGlucose扩展活动实现OnClickListener {最后弦乐LOG_TAG =myLogs;按钮btnAdd,btnRead,btnClear;
的EditText etGlucose,etTime,etDate; DBHelper dbHelper;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_add_glucose);
//显示操作栏中的向上按钮。
setupActionBar(); btnAdd =(按钮)findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(本); btnRead =(按钮)findViewById(R.id.btnRead);
btnRead.setOnClickListener(本); btnClear =(按钮)findViewById(R.id.btnClear);
btnClear.setOnClickListener(本); etGlucose =(EditText上)findViewById(R.id.etGlucose);
etTime =(EditText上)findViewById(R.id.etTime);
etDate =(EditText上)findViewById(R.id.etDate);
dbHelper =新DBHelper(本);
}/ **
*设置{@link android.app.ActionBar}。
* /
私人无效setupActionBar(){ getActionBar()setDisplayHomeAsUpEnabled(真)。}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.add_glucose,菜单);
返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
//这个ID重新presents主页或向上按钮。在这种情况下
//活动,向上按钮显示。使用NavUtils允许用户
//浏览了在应用结构中的一个级别。对于
//更多详细信息,请参见Android设计的导航模式:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(本);
返回true;
}
返回super.onOptionsItemSelected(项目);
}@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
ContentValues CV =新ContentValues();
串葡萄= etGlucose.getText()的toString()。
字符串时间= etTime.getText()的toString()。
。字符串日期= etDate.getText()的toString(); SQLiteDatabase分贝= dbHelper.getWritableDatabase(); 开关(v.getId()){
案例R.id.btnAdd:
Log.d(LOG_TAG ---插入mytable中:---); cv.put(葡萄糖,葡萄糖);
cv.put(时间,时间);
cv.put(日,日期);
长ROWID = db.insert(MYTABLE,空,CV);
Log.d(LOG_TAG,插入行,ID =+ ROWID);
打破; 案例R.id.btnRead:
Log.d(LOG_TAG---在MYTABLE行:---);
光标C = db.query(MYTABLE,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
如果(c.moveToFirst()){
INT idColIndex = c.getColumnIndex(ID);
INT glucoseColIndex = c.getColumnIndex(葡萄糖);
INT timeColIndex = c.getColumnIndex(时间);
INT dateColIndex = c.getColumnIndex(日期); 做{ Log.d(LOG_TAG,
ID =+ c.getInt(idColIndex)+
葡萄糖=+ c.getString(glucoseColIndex)+
时间=+ c.getString(timeColIndex)+,日期=+ c.getString(dateColIndex));
}而(c.moveToNext());
}其他
Log.d(LOG_TAG,0行);
c.close();
打破;案例R.id.btnClear:
Log.d(LOG_TAG---清除MYTABLE:---); INT clearCount = db.delete(MYTABLE,NULL,NULL);
Log.d(LOG_TAG,删除的行数=+ clearCount);
打破;
}
dbHelper.close();
} 类DBHelper扩展SQLiteOpenHelper { 公共DBHelper(上下文的背景下){ 超级(上下文中,MYTABLE,空,1);
} @覆盖
公共无效的onCreate(SQLiteDatabase DB){
Log.d(LOG_TAG ---的onCreate数据库---);
//создаемтаблицусполями
db.execSQL(创建表mytable(
+ID整数主键自动增量
+葡萄糖文字
+时文+号文+););
} @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){ }
}}
该错误只是说,该表 MYTABLE
不包含请求的列日期
。没有神奇的不止于此。
您应该删除并重新创建表,并确保,它有正确的列。请注意,您的onCreate
方法中包含一个错误。 A ,
是时间文字
后失踪。所以基本上你的数据库表中未即使在这种方法中创建的SQL是无效的。
尝试的onCreate创建表()
通过以下code:
db.execSQL(创建表mytable(
+ID整数主键自动增量
+葡萄糖文字
+时文//加了','
+号文+););
I'm trying to learn SQLite, trying basic things and (as expected) it doesn't really work.
I've tried to create another DB (different file), but the error remains the same.
03-05 17:20:18.989: D/myLogs(1323): --- Insert in mytable: ---
03-05 17:20:18.999: E/SQLiteLog(1323): (1) table mytable has no column named date
03-05 17:20:19.199: E/SQLiteDatabase(1323): Error inserting time=asd date=asd glucose=adf
03-05 17:20:19.199: E/SQLiteDatabase(1323): android.database.sqlite.SQLiteException: table mytable has no column named date (code 1): , while compiling: INSERT INTO mytable(time,date,glucose) VALUES (?,?,?)
here is the LogCat output: http://s15.postimage.org/soak9ifbf/androidlog.png (can't add images, have to give you a link)
Basically, it says, that there is no column named "date"
Here is the code:
package com.example.prototype2;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
public class AddGlucose extends Activity implements OnClickListener {
final String LOG_TAG = "myLogs";
Button btnAdd, btnRead, btnClear;
EditText etGlucose, etTime, etDate;
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_glucose);
// Show the Up button in the action bar.
setupActionBar();
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnRead = (Button) findViewById(R.id.btnRead);
btnRead.setOnClickListener(this);
btnClear = (Button) findViewById(R.id.btnClear);
btnClear.setOnClickListener(this);
etGlucose = (EditText) findViewById(R.id.etGlucose);
etTime = (EditText) findViewById(R.id.etTime);
etDate = (EditText) findViewById(R.id.etDate);
dbHelper = new DBHelper(this);
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_glucose, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
String glucose = etGlucose.getText().toString();
String time = etTime.getText().toString();
String date = etDate.getText().toString();
SQLiteDatabase db = dbHelper.getWritableDatabase();
switch (v.getId()) {
case R.id.btnAdd:
Log.d(LOG_TAG, "--- Insert in mytable: ---");
cv.put("glucose", glucose);
cv.put("time", time);
cv.put("date", date);
long rowID = db.insert("mytable", null, cv);
Log.d(LOG_TAG, "row inserted, ID = " + rowID);
break;
case R.id.btnRead:
Log.d(LOG_TAG, "--- Rows in mytable: ---");
Cursor c = db.query("mytable", null, null, null, null, null, null, null);
if (c.moveToFirst()) {
int idColIndex = c.getColumnIndex("id");
int glucoseColIndex = c.getColumnIndex("glucose");
int timeColIndex = c.getColumnIndex("time");
int dateColIndex = c.getColumnIndex("date");
do {
Log.d(LOG_TAG,
"ID = " + c.getInt(idColIndex) +
", glucose = " + c.getString(glucoseColIndex) +
", time = " + c.getString(timeColIndex) + ", date = " + c.getString(dateColIndex));
} while (c.moveToNext());
} else
Log.d(LOG_TAG, "0 rows");
c.close();
break;
case R.id.btnClear:
Log.d(LOG_TAG, "--- Clear mytable: ---");
int clearCount = db.delete("mytable", null, null);
Log.d(LOG_TAG, "deleted rows count = " + clearCount);
break;
}
dbHelper.close();
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "mytable", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(LOG_TAG, "--- onCreate database ---");
// создаем таблицу с полями
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement,"
+ "glucose text,"
+ "time text" + "date text" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
The error just says, that the table mytable
does not contain the requested column date
. No magic beyond this.
You should drop and create the table again and ensure, that it has the right columns. Please note that your onCreate
Method contains an error. A ,
is missing after time text
. So basically your DB table is not even created in this method as the SQL is invalid.
Try creating the table in onCreate()
with the following code:
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement,"
+ "glucose text,"
+ "time text," // added a ','
+ "date text" + ");");
这篇关于"表*已经没有列名为*"错误的SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!