databasehelper CRUD功能

时间:2019-01-13 20:03:37

标签: java sqlite

我是android studio的新手,并已复制示例代码示例以适合我的应用程序。目的是阅读我所有的公司记录。我尝试了几种方法,但均未成功。我收到的错误指示无法将Customer()应用于:下面的字段列表。我有12个表,每个表都有CRUD方法。所有表的相同错误是相同的。

我已经构建了所有单独的table.java文件,并希望在继续操作main.java文件之前完成所有CRUD功能而不会出错。

// Get All CompanyData =========================================================
public List<CompanyData> GetAllCompanyData() {
    List<CompanyData> compdataList = new ArrayList<>();
    String selectQuery = "SELECT * FROM " + TABLE_CompanyData;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            CompanyData compdata;
            compdata = new CompanyData();
                    compdata.setcompanyid(Long.parseLong(cursor.getString(0)));     // <==============================================================
                    compdata.setcompanyname(cursor.getString(1));
                    compdata.setcompanyaddress1(cursor.getString(2));
                    compdata.setcompanyaddress2(cursor.getString(3));
                    compdata.setcompanycity(cursor.getString(4));
                    compdata.setcompanystate(cursor.getString(5));
                    compdata.setcompanyzipcode(cursor.getString(6));
                    compdata.setcompanyphone(cursor.getString(7));
                    compdata.setcompanyemail(cursor.getString(8));
                    compdata.setcompanybusinesslicense(cursor.getString(9));
                    compdata.setcompanytype(cursor.getString(10));
            compdataList.add(compdata);
        } while (cursor.moveToNext());
    }
    return compdataList;
}

1 个答案:

答案 0 :(得分:0)

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="12dp"
        android:gravity="center"
        android:text="Username"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView"
        android:ems="10"
        android:gravity="center_vertical|center"
        android:hint="Enter Name"
        android:inputType="textPersonName"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editName"
        android:layout_marginTop="13dp"
        android:gravity="center"
        android:hint="Enter Password"
        android:text="password"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button"
        android:layout_alignBottom="@+id/button"
        android:layout_alignEnd="@+id/button4"
        android:layout_alignRight="@+id/button4"
        android:onClick="viewdata"
        android:text="view data"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editPass"
        android:layout_marginLeft="28dp"
        android:layout_marginStart="28dp"
        android:layout_marginTop="23dp"
        android:onClick="addUser"
        android:text="add user"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button4"
        android:layout_alignStart="@+id/button4"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="13dp"
        android:onClick="update"
        android:text="Update"
        android:textStyle="normal|bold" />

    <EditText
        android:id="@+id/editText6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/button4"
        android:layout_toLeftOf="@+id/button2"
        android:layout_toStartOf="@+id/button2"
        android:ems="10"
        android:freezesText="false"
        android:hint="Enter Name to Delete Data"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="41dp"
        android:layout_marginEnd="21dp"
        android:layout_marginRight="21dp"
        android:onClick="delete"
        android:text="delete"
        android:textStyle="normal|bold"
        tools:ignore="RelativeOverlap" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/button"
        android:layout_marginLeft="7dp"
        android:layout_marginStart="7dp"
        android:layout_marginTop="47dp"
        android:ems="10"
        android:hint="Current Name"
        android:inputType="textPersonName"
        android:textSize="14sp"
        android:textStyle="bold|italic" />

    <EditText
        android:id="@+id/editPass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="11dp"
        android:ems="10"
        android:gravity="center_vertical|center"
        android:hint="Enter Password"
        android:inputType="textPassword"
        android:textAllCaps="false"
        android:textSize="18sp"
        android:textStyle="normal|bold" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText3"
        android:layout_alignStart="@+id/editText3"
        android:layout_alignTop="@+id/button3"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:hint="New Name"
        android:inputType="textPersonName"
        android:textSize="14sp"
        android:textStyle="bold|italic" />

</RelativeLayout>'

main activityjava

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends Activity {
    EditText Name, Pass , updateold, updatenew, delete;
    DbAdapter helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Name= (EditText) findViewById(R.id.editName);
        Pass= (EditText) findViewById(R.id.editPass);
        updateold= (EditText) findViewById(R.id.editText3);
        updatenew= (EditText) findViewById(R.id.editText5);
        delete = (EditText) findViewById(R.id.editText6);

        helper = new DbAdapter(this);
    }
    public void addUser(View view)
    {
        String t1 = Name.getText().toString();
        String t2 = Pass.getText().toString();
        if(t1.isEmpty() || t2.isEmpty())
        {
            Message.message(getApplicationContext(),"Enter Both Name and Password");
        }
        else
        {
            long id = helper.insertData(t1,t2);
            if(id<=0)
            {
                Message.message(getApplicationContext(),"Insertion Unsuccessful");
                Name.setText("");
                Pass.setText("");
            } else
            {
                Message.message(getApplicationContext(),"Insertion Successful");
                Name.setText("");
                Pass.setText("");
            }
        }
    }

    public void viewdata(View view)
    {
        String data = helper.getData();
        Message.message(this,data);
    }

    public void update( View view)
    {
        String u1 = updateold.getText().toString();
        String u2 = updatenew.getText().toString();
        if(u1.isEmpty() || u2.isEmpty())
        {
            Message.message(getApplicationContext(),"Enter Data");
        }
        else
        {
            int a= helper.updateName( u1, u2);
            if(a<=0)
            {
                Message.message(getApplicationContext(),"Unsuccessful");
                updateold.setText("");
                updatenew.setText("");
            } else {
                Message.message(getApplicationContext(),"Updated");
                updateold.setText("");
                updatenew.setText("");
            }
        }

    }
    public void delete( View view)
    {
        String uname = delete.getText().toString();
        if(uname.isEmpty())
        {
            Message.message(getApplicationContext(),"Enter Data");
        }
        else{
            int a= helper.delete(uname);
            if(a<=0)
            {
                Message.message(getApplicationContext(),"Unsuccessful");
                delete.setText("");
            }
            else
            {
                Message.message(this, "DELETED");
                delete.setText("");
            }
        }
    }
}'

DbAdapterjava


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

public class DbAdapter extends SQLiteOpenHelper {

     private static final String DATABASE_NAME = "myDatabase";    // Database Name
        private static final String TABLE_NAME = "myTable";   // Table Name
        private static final int DATABASE_Version = 1;   // Database Version
        private static final String UID = "_id";     // Column I (Primary Key)
        private static final String NAME = "Name";    //Column II
        private static final String MyPASSWORD = "Password";    // Column III
        private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
                " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(255) ," + MyPASSWORD + " VARCHAR(225));";
        private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
        private Context context;


    public DbAdapter(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_Version);
        this.context = context;
    }


    public long insertData(String name, String pass) {
        SQLiteDatabase dbb = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(NAME, name);
        contentValues.put(MyPASSWORD, pass);
        long id = dbb.insert(TABLE_NAME, null, contentValues);
        return id;
    }

    public String getData() {
        SQLiteDatabase db = getWritableDatabase();
        String[] columns = {UID, NAME, MyPASSWORD};
        Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
        StringBuffer buffer = new StringBuffer();
        while (cursor.moveToNext()) {
            int cid = cursor.getInt(cursor.getColumnIndex(UID));
            String name = cursor.getString(cursor.getColumnIndex(NAME));
            String password = cursor.getString(cursor.getColumnIndex(MyPASSWORD));
            buffer.append(cid + "   " + name + "   " + password + " \n");
        }
        return buffer.toString();
    }

    public int delete(String uname) {
        SQLiteDatabase db = this.getWritableDatabase();
        String[] whereArgs = {uname};

        int count = db.delete(TABLE_NAME, NAME + " = ?", whereArgs);
        return count;
    }

    public int updateName(String oldName, String newName) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(NAME, newName);
        String[] whereArgs = {oldName};
        int count = db.update(TABLE_NAME, contentValues, NAME + " = ?", whereArgs);
        return count;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        try {
            db.execSQL(CREATE_TABLE);
        } catch (Exception e) {
            Message.message(context, "" + e);
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        try {
            Message.message(context, "OnUpgrade");
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (Exception e) {
            Message.message(context, "" + e);
        }

    }

}'

    Messagejava


    import android.content.Context;
    import android.widget.Toast;

    public class Message {
        public static void message(Context context, String message) {
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }
    }'