table没有名为PosY的列(代码1):,编译时:INSERT INTO Sample(Name,No,PosY,Img,PosX)VALUES(?,?,?,?,?)

时间:2015-03-27 12:34:56

标签: android sqlite

我正在创建一个数据库并尝试插入和检索值。但是当我尝试插入值时,Logcat上显示以下错误

table Sample没有名为PosY的列(代码1):,编译时:INSERT INTO Sample(Name,No,PosY,Img,PosX)VALUES(?,?,?,?,?)

这是SQLiteOpenHelper类的代码

公共类SQL扩展了SQLiteOpenHelper {

public static final String DB_NAME="New.db";
public static final String TABLE_NAME="Sample";
public static final int DB_VERSION=1;

public static final String NAME="Name";
public static final String NO="No";
public static final String IMG="Img";
public static final String PIXELX="PosX";
public static final String PIXELY="PosY";

Cursor cur;
byte[] blob;
String name;
String no;
int pixelx,pixely;
Bitmap bmp;

Context ctxt;

private SQLiteDatabase mDb;

public SQL(Context context)
{
    super(context,DB_NAME,null,1);
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE "+TABLE_NAME+"("+NAME +" TEXT "+","+ NO +" TEXT PRIMARY KEY ,"+IMG +" BLOB,"+PIXELX +" INTEGER, "+ PIXELY +"INTEGER);");
}
public void onUpgrade(SQLiteDatabase db,int old,int newversion)
{
    db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME+";");
    onCreate(db);
}
public void drop(SQLiteDatabase db)
{
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME+" ;");
}
public void open(SQLiteDatabase db)
{
    db=this.getWritableDatabase();
}
public void close(SQLiteDatabase db)
{
    db.close();
}

public void insert(SQLiteDatabase db,String name,String no,byte[] ImgData,int pixelx,int pixely)
{
    db=this.getWritableDatabase();
    ContentValues con=new ContentValues();
    con.put(NAME, name);
    con.put(NO, no);
    con.put(IMG, ImgData);
    con.put(PIXELX, pixelx);
    con.put(PIXELY, pixely);
    db.insert(TABLE_NAME, null, con);
}


@SuppressLint("NewApi") public User  retrieve  (SQLiteDatabase db,String name) throws Exception
{
    cur=db.query(true,TABLE_NAME, new String[]{NAME,NO,IMG,PIXELX,PIXELY},"NAME="+name+" ", null,null, null,null, null,null);
    //cur=db.query(TABLE_NAME, new String[]{IMG},null, null,null, null, null);

    if(cur.moveToFirst())
    {
        name=cur.getString(cur.getColumnIndex(NAME));
        no=cur.getString(cur.getColumnIndex(NO));
         blob=cur.getBlob(cur.getColumnIndex(IMG));
         pixelx=cur.getInt(cur.getColumnIndex(PIXELX));
         pixely=cur.getInt(cur.getColumnIndex(PIXELY));
        cur.close();
        return new User(name,no,Utility.getPhoto(blob),pixelx,pixely);
    }
        cur.close();
        return null;
}

}

我尝试将数据插入数据库的代码是

public class Register extends Activity {
     ImageView img;
     String picpath;
     Bitmap bmp;
     Canvas cnvs;
     Random r;
     int x,y,height,width;
     ByteArrayOutputStream blob;
     SQL sql;
     Paint paint;
     String name,no;
     Uri data;
     SQLiteDatabase db;
     String[] filePath={MediaStore.Images.Media.DATA};;
     Cursor cur;
     TextView txt;
     int colIndex;
     byte[] imgData;

    public Register()
    {

    }

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);



        img=(ImageView)findViewById(R.id.imageView1);
        sql=new SQL(this);
        db=sql.getWritableDatabase();
        sql.drop(db);
        sql.onCreate(db);
        r=new Random();
        paint=new Paint();
        x=r.nextInt(100);
        blob=new ByteArrayOutputStream();


        Intent i=getIntent();
        name=i.getStringExtra("name");
        no=i.getStringExtra("no");
        //Open Button
        Button open=(Button)findViewById(R.id.button3);
        open.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent gal_open=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gal_open,1);

            }
        });
        txt=(TextView)findViewById(R.id.textView1);

        Button shuffle=(Button)findViewById(R.id.button2);
        shuffle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565);
                cnvs=new Canvas(bmp);

                height=img.getMeasuredHeight();
                width=img.getMeasuredWidth();
                x=r.nextInt(height);
                y=r.nextInt(width);
                paint.setColor(Color.RED);

                //data=intentData.getData();

                //cur=getContentResolver().query(data,filePath,null,null,null );
                //cur.moveToFirst();

                //colIndex=cur.getColumnIndex(filePath[0]);
                //picpath=cur.getString(colIndex);
                //cur.close();
                txt.setText(x+","+y);
                cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
                cnvs.drawRect(x, y,x+20,y+20 , paint);
                img.setImageBitmap(bmp);
                 /*height=img.getMeasuredHeight();
                 width=img.getWidth();

                //Random r=new Random();
                x=r.nextInt(width);
                y=r.nextInt(height);

                cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
                cnvs.drawRect(x, y,x+50,y+50 , paint);
                img.setImageBitmap(bmp);*/

            }
        });
        Button ok= (Button)findViewById(R.id.button1);
        txt=(TextView)findViewById(R.id.textView1);

        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                txt.setText(x+"'"+y);
                bmp.compress(CompressFormat.JPEG,100, blob);
                imgData=blob.toByteArray();
                sql.insert(db,name,no,imgData,x,y); 

            }
        });
    }
    public void onDraw()
    {

    }
    public void onActivityResult(int requestCode,int resultCode,Intent intentData)
    {
        super.onActivityResult(requestCode, resultCode, intentData);
        if(requestCode==1 && resultCode==RESULT_OK && intentData!=null)
        {


            bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565);
            cnvs=new Canvas(bmp);

            height=img.getMeasuredHeight();
            width=img.getMeasuredWidth();
            x=r.nextInt(height);
            y=r.nextInt(width);
            paint.setColor(Color.RED);

            data=intentData.getData();

            cur=getContentResolver().query(data,filePath,null,null,null );
            cur.moveToFirst();

            colIndex=cur.getColumnIndex(filePath[0]);
            picpath=cur.getString(colIndex);
            cur.close();

            txt=(TextView)findViewById(R.id.textView1);
            txt.setText(x+","+y);
            cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
            cnvs.drawRect(x, y,x+20,y+20 , paint);
            img.setImageBitmap(bmp);





        }   

    }


}
你能说出错的地方吗?提前致谢

2 个答案:

答案 0 :(得分:2)

而不是PIXELY +"INTEGER您需要PIXELY +" INTEGER(缺少空格)

答案 1 :(得分:0)

使用此

db.execSQL("CREATE TABLE "+TABLE_NAME+"("+NAME +" TEXT "+","+ NO +" TEXT PRIMARY KEY ,"+IMG +" BLOB,"+PIXELX +" INTEGER, "+ PIXELY +" INTEGER);");

而不是

db.execSQL("CREATE TABLE "+TABLE_NAME+"("+NAME +" TEXT "+","+ NO +" TEXT PRIMARY KEY ,"+IMG +" BLOB,"+PIXELX +" INTEGER, "+ PIXELY +"INTEGER);");

您错过了在整数类型

之前添加空格