SQLite addData无法应用于给定类型

时间:2018-05-25 19:13:24

标签: android sqlite android-studio android-sqlite

我刚刚开始学习编程,并且正在尝试使用SQLite数据库编写Android应用程序。我遇到了一个编译错误,我真的不明白如何修复。

尝试运行应用时出现以下错误:

error: method addData in class DatabaseHelper cannot be applied to given types;
required: String,String,String,String,String,String
found: String
reason: actual and formal argument lists differ in length

以下是数据库助手的代码:

package com.tt.www.ca2;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "CannApicius1";

public static final String TAG = "DatabaseHelper";

public static final String TABLE_NAME1 = "Users";
public static final String USERID = "UserID";
public static final String USERNAME = "UserName";
public static final String NAME1 = "FirstName";
public static final String NAME2 = "LastName";
public static final String COMPANYNAME = "Company";
public static final String USEREMAIL = "EmailAddress";

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

}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "create table " + TABLE_NAME1 + "(USERID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME Text, NAME1 Text, NAME2 Text, COMPANYNAME Text, USEREMAIL Text)";
    db.execSQL(createTable);
}

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

public boolean addData(String UserID, String UserName, String FirstName, String LastName, String Company, String EmailAddress) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(USERID,UserID);
    contentValues.put(USERNAME,UserName);
    contentValues.put(NAME1,FirstName);
    contentValues.put(NAME2,LastName);
    contentValues.put(COMPANYNAME, Company);
    contentValues.put(USEREMAIL,EmailAddress);
    Log.d(TAG, "addData: Adding" + UserID + " to " + TABLE_NAME1);
    long result = db.insert(TABLE_NAME1,null,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

}

以下是我正在尝试保存的数据的活动的java代码:

package com.tt.www.ca2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class EditUserInfo extends AppCompatActivity {
DatabaseHelper myDb;
TextView seeUserID;
EditText editUsername,editFirstName,editLastName,editCompanyName,editEmail;
ImageButton btnsaveuserdata;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_user_info);
    seeUserID = (TextView) findViewById(R.id.userIDview);
    editUsername = (EditText) findViewById(R.id.username);
    editFirstName = (EditText) findViewById(R.id.name1);
    editLastName = (EditText) findViewById(R.id.name2);
    editCompanyName = (EditText) findViewById(R.id.companyname);
    editEmail = (EditText) findViewById(R.id.emailaddress);
    btnsaveuserdata = (ImageButton) findViewById(R.id.savesettingsbtn);
    myDb = new DatabaseHelper(this);
    btnsaveuserdata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String newEntry = seeUserID.getText().toString();
            editUsername.getText().toString();
            editFirstName.getText().toString();
            editLastName.getText().toString();
            editLastName.getText().toString();
            editCompanyName.getText().toString();
            editEmail.getText().toString();
        if (editUsername.length() != 0) {
            AddData(newEntry);
            editUsername.setText("");
        } else {
            toastMessage("You must put something in the username field");
        }
        }
    });
}

public void AddData(String newEntry) {
    boolean insertData = myDb.addData(newEntry);
    if (insertData) {
        toastMessage("Data Inserted Successfully");
    }   else    {
        toastMessage("Something Went Wrong!");
    }

}

private void toastMessage(String message){
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
}

代码编辑器标识“boolean insertData = myDb.addData(newEntry);”行。作为

错误的来源,但我正在努力弄清楚如何让mbDb找到所有数据字符串。

1 个答案:

答案 0 :(得分:0)

addData() DatabaseHelper.java中的 public boolean addData(String UserID, String UserName, String FirstName, String LastName, String Company, String EmailAddress)/*<-Over Here*/ { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(USERID,UserID); contentValues.put(USERNAME,UserName); contentValues.put(NAME1,FirstName); contentValues.put(NAME2,LastName); contentValues.put(COMPANYNAME, Company); contentValues.put(USEREMAIL,EmailAddress); Log.d(TAG, "addData: Adding" + UserID + " to " + TABLE_NAME1); long result = db.insert(TABLE_NAME1,null,contentValues); if(result == -1) return false; else return true; } 函数需要6个字符串类型参数如下所示

addData()

但是当你在EditUserInfo.java上调用那个(public void AddData(String newEntry) { boolean insertData = myDb.addData(newEntry);/*<-Over Here*/ if (insertData) { toastMessage("Data Inserted Successfully"); } else { toastMessage("Something Went Wrong!"); } } )函数时,你只传递了下面的一个参数: -

AddData():-

<强>解决方案: -

<强> public void AddData(String userId, String username, String name1, String name2, String companyName, String userEmail) { boolean insertData = myDb.addData(userId,username,name1,name2,companyName,userEmail); if (insertData) { toastMessage("Data Inserted Successfully"); } else { toastMessage("Something Went Wrong!"); } }

您需要添加更多类似的参数: -

onCreate()

<强> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit_user_info); seeUserID = (TextView) findViewById(R.id.userIDview); editUsername = (EditText) findViewById(R.id.username); editFirstName = (EditText) findViewById(R.id.name1); editLastName = (EditText) findViewById(R.id.name2); editCompanyName = (EditText) findViewById(R.id.companyname); editEmail = (EditText) findViewById(R.id.emailaddress); btnsaveuserdata = (ImageButton) findViewById(R.id.savesettingsbtn); myDb = new DatabaseHelper(this); btnsaveuserdata.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userId= seeUserID.getText().toString(); String username = editUsername.getText().toString(); String name1 = editFirstName.getText().toString(); String name2 = editLastName.getText().toString(); String companyName = editCompanyName.getText().toString(); String userEmail = editEmail.getText().toString(); if (editUsername.getText().toString().equals("")) { AddData(userId,username,name1,name2,companyName,userEmail); editUsername.setText(""); } else { toastMessage("You must put something in the username field"); } } }); } : -

{{1}}