YXTS122 2017-02-28 21:58 采纳率: 100%
浏览 1436
已采纳

安卓,好像是sql语句报错了,怎么改

图片说明
图片说明
图片说明
DBHelper.java

 package com.example.order;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.widget.Toast;

public class DBHelper extends SQLiteOpenHelper{
    private Context mContext;
    public DBHelper(Context context,String name,CursorFactory factory,int version)
    {
        super(context,name,factory,version);
        mContext=context;
    }

    public void onCreate(SQLiteDatabase db)
    {
        String DATABASE_CREATE="create table orderdetail(_id integer primary key autoincrement,orderid text,detailName text,num text,detailid text,menuid text,sate text,price text);";
        db.execSQL(DATABASE_CREATE);
        Toast.makeText(mContext,"创建成功!",Toast.LENGTH_LONG).show();
    }

    public void onUpgrade(SQLiteDatabase db,int arg1,int arg2)
    {
        String sql="DROP TABLE IF EXISTS orderdetail";
        db.execSQL(sql);
        this.onCreate(db);
    }

}

MainActivity.java

 package com.example.order;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
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.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    private EditText tdetailid,torderid,tmenuid,tdetailName,tnum,tsate,tprice,morderid;
    private Button insert,search;
    DBHelper dbHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper=new DBHelper(this,"orderdetail.db",null,2);
        insert=(Button)findViewById(R.id.insert);
        insert.setOnClickListener(this);
        search=(Button)findViewById(R.id.search);
        search.setOnClickListener(this);
        tdetailid=(EditText)findViewById(R.id.et1);
        torderid=(EditText)findViewById(R.id.et2);
        tmenuid=(EditText)findViewById(R.id.et3);
        tdetailName=(EditText)findViewById(R.id.et4);
        tnum=(EditText)findViewById(R.id.et5);
        tsate=(EditText)findViewById(R.id.et6);
        tprice=(EditText)findViewById(R.id.et7);
        morderid=(EditText)findViewById(R.id.et8);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.insert:
            String forderid=torderid.getText().toString().trim();
            String fdetailName=tdetailName.getText().toString().trim();
            String fnum=tnum.getText().toString().trim();
            String fdetailid=tdetailid.getText().toString().trim();
            String fmenuid=tmenuid.getText().toString().trim();
            String fsate=tsate.getText().toString().trim();
            String fprice=tprice.getText().toString().trim();
            db=dbHelper.getWritableDatabase();
            db.execSQL("insert into orderdetail(forderid,fdetailName,fnum,fdetailid,fmenuid,fsate,fprice) values(?,?,?,?,?,?,?)",new String[]{forderid,fdetailName,fnum,fdetailid,fmenuid,fsate,fprice});
            Toast.makeText(this, "添加成功!", Toast.LENGTH_LONG).show();
            break;
        case R.id.search:
            String norderid=morderid.getText().toString().trim();
            Bundle data=new Bundle();
            Cursor curso = dbHelper.getReadableDatabase().rawQuery("select * from orderdetail where orderID="+norderid,null );
            List<OrderDetail> OrderList = new ArrayList<OrderDetail>();
            while(curso.moveToNext()){
                int detailid = curso.getInt(curso.getColumnIndex("orderdetialid"));
                String orderid = curso.getString(curso.getColumnIndex("orderID"));
                int menuid = curso.getInt(curso.getColumnIndex("menuid"));
                String detailName = curso.getString(curso.getColumnIndex("detailName"));
                int num = curso.getInt(curso.getColumnIndex("quantity"));
                int sate = curso.getInt(curso.getColumnIndex("detailsate"));
                int price =curso.getInt(curso.getColumnIndex("price"));
                OrderDetail od = new OrderDetail(detailid,orderid,menuid,detailName,num,sate,price);
                OrderList.add(od);
            }

            data.putSerializable("orderid",norderid );
            Intent intent=new Intent(MainActivity.this,OrderActivity.class);
            intent.putExtras(data);
            startActivity(intent);
            break;
        default:break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

  • 写回答

8条回答 默认 最新

  • ejb99666 2017-03-01 00:43
    关注

    报错写着,orderdetail表少字段forderid

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补