- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果单击下一个按钮,我正在尝试一一显示数据库中的数据。我只是想检查程序是否可以从数据库中选择第一个数据,但我收到了错误消息。
Caused by: android.database.sqlite.SQLiteException: no such column: color (code 1): , while compiling: SELECT DISTINCT _id, brand, model, price, color, img FROM car WHERE _id=1
DBAdapter.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Asus on 24.04.2015.
*/
public class DBAdapter {private DatabaseHelper DBHelper;
private SQLiteDatabase db;
private final Context context;
public static final String KEY_ROWID = "_id";
public static final String KEY_BRAND = "brand";
public static final String KEY_MODEL = "model";
public static final String KEY_PRICE = "price";
public static final String KEY_COLOR = "color";
public static final String KEY_PHOTO = "img";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "testDB";
private static final String DATABASE_TABLE = "car";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table car (_id integer primary key autoincrement, "
+ "brand text, model text, price integer"
+ "color text, img blob);";
// Constructor
public DBAdapter(Context context) {
this.context = context;
DBHelper = new DatabaseHelper(context);
}
// To create and upgrade a database in an Android application SQLiteOpenHelper subclass is usually created
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// onCreate() is only called by the framework, if the database does not exist
Log.d("Create", "Creating the database");
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// onUpgrade() is only called by the framework, if one changes the database version number
// Sends a Warn log message
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
// Method to execute an SQL statement directly
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
// Opens the database
public DBAdapter open() throws SQLException {
// Create and/or open a database that will be used for reading and writing
//db = DBHelper.getWritableDatabase();
// Use if you only want to read data from the database
db = DBHelper.getReadableDatabase();
return this;
}
// Closes the database
public void close() {
// Closes the database
DBHelper.close();
}
// Insert a contact into the database
public long insertContact(int rowid, String brand, String model, int price, String color, byte [] photo) {
// The class ContentValues allows to define key/values. The "key" represents the
// table column identifier and the "value" represents the content for the table
// record in this column. ContentValues can be used for inserts and updates of database entries.
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ROWID, rowid);
initialValues.put(KEY_BRAND, brand);
initialValues.put(KEY_MODEL, model);
initialValues.put(KEY_PRICE, price);
initialValues.put(KEY_COLOR, color);
initialValues.put(KEY_PHOTO, photo);
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Retrieves a particular contact
public Cursor getContact(int id) throws SQLException {
// rawQuery() directly accepts an SQL select statement as input.
// query() provides a structured interface for specifying the SQL query.
// A query returns a Cursor object. A Cursor represents the result of a query
// and basically points to one row of the query result. This way Android can buffer
// the query results efficiently; as it does not have to load all data into memory
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,KEY_BRAND, KEY_MODEL, KEY_PRICE, KEY_COLOR, KEY_PHOTO}, KEY_ROWID + "=" + id, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
DisplayActivity.java
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DisplayActivity extends Activity {
private String [] car_images;
private TextView carid;
private TextView brand;
private TextView model;
private TextView price;
private TextView color;
private ImageView photo;
private DBAdapter db;
private Bitmap mBitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.display);
car_images = getResources().getStringArray(R.array.car_images);
carid = (TextView) findViewById(R.id.caridDB);
brand = (TextView) findViewById(R.id.brandDB);
model = (TextView) findViewById(R.id.modelDB);
price = (TextView) findViewById(R.id.priceDB);
color = (TextView) findViewById(R.id.colorDB);
photo = (ImageView) findViewById(R.id.photoDB);
// Database will be created using the DBAdapter
db = new DBAdapter(this);
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mito);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
db.open();
long id = db.insertContact(1, "Alfa Romeo", "Mito", 2900, "Red",image);
// long id = db.insertContact("Larry", "page", 3000000, image);
// long id = db.insertContact("Steve", "Ballmer", 5000000, image);
Cursor c = db.getContact(1);
if (c.moveToFirst())
DisplayContact(c);
else
Toast.makeText(this, "No record found", Toast.LENGTH_LONG)
.show();
db.close();
}
public void onClick(View view) {
// Get a contact
db.open();
Cursor c = db.getContact(Integer.parseInt(carid.getText().toString()));
if (c.moveToFirst())
DisplayContact(c);
else
Toast.makeText(this, "No record found", Toast.LENGTH_LONG)
.show();
db.close();
}
public void DisplayContact(Cursor c) {
carid.setText(c.getString(0));
brand.setText(c.getString(1));
model.setText(c.getString(2));
price.setText(c.getString(3));
color.setText(c.getString(4));
if (Integer.parseInt(c.getString(0))==0){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mito);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap); }
else if (Integer.parseInt(c.getString(0))==1){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.giulettaa);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else if (Integer.parseInt(c.getString(0))==2){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.alfa4c);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else if (Integer.parseInt(c.getString(0))==3){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gran);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else if (Integer.parseInt(c.getString(0))==4){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.coupe);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else if (Integer.parseInt(c.getString(0))==5){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.c3);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else if (Integer.parseInt(c.getString(0))==6){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.c4);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else if (Integer.parseInt(c.getString(0))==7){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gla);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else if (Integer.parseInt(c.getString(0))==8){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.smart);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else if (Integer.parseInt(c.getString(0))==9){
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.node);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
else {
// Add a contact
// A Bitmap must be converted to a byte array so that it can be stored in a blob
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.juke);
// mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.google);
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mByteArrayOutputStream);
byte[] image = mByteArrayOutputStream.toByteArray();
image = c.getBlob(5);
Bitmap mBitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
photo.setImageBitmap(mBitmap);
}
}
}
最佳答案
你的问题就在这里
private static final String DATABASE_CREATE = "create table car (_id integer primary key autoincrement, "
+ "brand text, model text, price integer" // <-- NO COMMA after 'integer'!
+ "color text, img blob);";
你少了一个逗号。应该是:
private static final String DATABASE_CREATE = "create table car (_id integer primary key autoincrement, "
+ "brand text, model text, price integer," // <-- COMMA
+ "color text, img blob);";
关于java - Android SQL 数据库错误(代码 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29870178/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!