值不会插入到数据库查询中返回false值 [英] Values are not getting insert into database query returning false value

查看:71
本文介绍了值不会插入到数据库查询中返回false值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package com.example.puja.pizzahut;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class PizzaDetailSubActivity extends AppCompatActivity {

    EditText qty;
    TimePicker timePicker2;
    InsertDatabaseHandler db;
    TextView datalayoutaddress;
    String registeration_name,registeration_username,registeration_password,registeration_phone,pizza_name1,pizza_price,mode_of_trnf,reg_address,time_,reg_qty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pizza_detail_sub);
        Bundle bundle=getIntent().getExtras();
        registeration_name=bundle.getString("name");
        registeration_username=bundle.getString("username");
        registeration_password=bundle.getString("password");
        registeration_phone=bundle.getString("phone");
        pizza_name1=bundle.getString("pizza_name");
        pizza_price=bundle.getString("pizza_price");
        mode_of_trnf=bundle.getString("mode_of_trnf");
        db=new InsertDatabaseHandler(this);
        timePicker2=(TimePicker)findViewById(R.id.timePicker2);
        datalayoutaddress=(TextView)findViewById(R.id.address);
        qty=(EditText)findViewById(R.id.qty);
    }

    public void onClickBuyNow(View view) {
        reg_address = datalayoutaddress.getText().toString();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            time_ = timePicker2.getHour() + ":" + timePicker2.getMinute();
            reg_qty = qty.getText().toString();
            System.out.println("Buy Time:" + timePicker2.getHour() + ":" + timePicker2.getMinute());
            System.out.println(registeration_name + registeration_username + registeration_password + registeration_phone + pizza_name1 + pizza_price + mode_of_trnf + reg_address + time_ + reg_qty);
             boolean result = db.insertCustRecord(registeration_name,registeration_phone,pizza_name1,pizza_price,mode_of_trnf,reg_address,time_,reg_qty);
             System.out.println(result);
             startActivity(new Intent(this,ViewCart.class));

            if (result == true){
                System.out.println("Cust data inserted ...");
                Toast.makeText(PizzaDetailSubActivity.this,"Thank You Visit Again...",Toast.LENGTH_SHORT).show();
            }
            else {
                System.out.println("Cust data cannot be inserted ...");
            }

        }
    }


    public void onClickAddToCart(View view){
        reg_address=datalayoutaddress.getText().toString();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Toast.makeText(this,timePicker2.getHour()+":"+timePicker2.getMinute(),Toast.LENGTH_SHORT).show();
            time_=timePicker2.getHour()+":"+timePicker2.getMinute();
            reg_qty=qty.getText().toString();
            System.out.println("Cart Time:"+timePicker2.getHour()+":"+timePicker2.getMinute());
            System.out.println("Quantity : "+reg_qty);
            System.out.println(  registeration_name+registeration_username+registeration_password+registeration_phone+pizza_name1+pizza_price+mode_of_trnf+reg_address+time_+reg_qty);
        }
    }


    public void onClickOffers(View view){

    }

    public void onClickBack(View view){
      new Intent(this,PizzaDetailss.class);
    }

}




package com.example.puja.pizzahut;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class InsertDatabaseHandler extends SQLiteOpenHelper{
    private final static String TABLE_NAME2="customer";


    private final static String DATABASE_NAME = "PIZZA.DB";
    private final static String CartCOL1 = "ID";
    private final static String CartCol2="NAME";
    private final static String CartCol3="USERNAME";
    private final static String CartCol4="PASSWORD";
    private final static String CartCol5="PHONE";
    private final static String CartCol6="PIZZA_NAME";
    private final static String CartCol7="PRICE";
    private final static String CartCol8="MODE_OF_TRANS";
    private final static String CartCol9="ADDRESS";
    private final static String CartCol10="TIME";
    private final static String CartCol11="QUANTITY";


    public InsertDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME2 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PHONE INTEGER,PIZZA_NAME TEXT,PRICE INTEGER,MODE_OF_TRANS TEXT,ADDRESS TEXT,TIME TEXT,QUANTITY INTEGER)");
        System.out.println("Customer Table Created ...");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME2);
    }

    public boolean insertCustRecord(String name,String phone,String pizza_name ,String price,String mode,String address,String time_,String qty){
        System.out.println(name+phone);
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(CartCol2, name);
        contentValues.put(CartCol5, phone);
        contentValues.put("PIZZA_NAME", pizza_name);
        contentValues.put("PRICE", price);
        contentValues.put("MODE_OF_TRANS", mode);
        contentValues.put("ADDRESS", address);
        contentValues.put("TIME", time_);
        contentValues.put("QUANTITY", qty);
         long value=db.insert(TABLE_NAME2, null, contentValues);
        db.close();
        if (value == -1)
            return false;
        else
            return true;
    }


    public Cursor getCustData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME2, null);
        return cursor;
    }

}





我尝试过: < br $>


PizzaDetailSubAvtivity是我的类连接到数据库类InsertDatabaseHandler

在PizzaDetailSubActivity中的onClickBuyNow方法将值传递给数据库方法insertCustRecord但insertCustRecord返回false值尽管所有值不是null



What I have tried:

PizzaDetailSubAvtivity is my class connected to database class InsertDatabaseHandler
onClickBuyNow method within PizzaDetailSubActivity passes values to database method insertCustRecord but insertCustRecord returning false value though all values are not null

推荐答案

我们无法帮助您 - 它需要您的数据,并在代码运行该数据时查看正在发生的事情。我们没有您的数据,我们没有您的整个应用程序 - 如果我们这样做,我们也不知道如何使用它!



所以,它取决于你。

在函数的第一行放置断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
We can't help you with this - it needs your data, and to be looking at what is happening while the code is running on that data. And we don't have your data, we don't have your whole app - and we don't know how to use it if we did!

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于值不会插入到数据库查询中返回false值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆