简体   繁体   English

数据库无法解析为变量

[英]Database cannot be resolved to a variable

There was an Error in creating the Database and it was indicating the error word "Database" and it was displaying error: 创建数据库时发生错误,它指示错误词“数据库”,并且显示错误:

Database cannot be resolved a variable

So please tell me how to rectify it. 所以,请告诉我如何纠正它。

package com.flocater;
import com.flocator.R;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddFriends extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addfriends);
    }

    public void addFriend(View v) {
        // get access to views
        EditText editName = (EditText) this.findViewById(R.id.editName1);
        EditText editPhone = (EditText) this.findViewById(R.id.editPhone1);
        EditText editArea = (EditText) this.findViewById(R.id.editArea1);
        EditText editCity = (EditText) this.findViewById(R.id.editCity1);

        try {
            DBHelper dbhelper = new DBHelper(this);
            SQLiteDatabase db = dbhelper.getWritableDatabase();
            Log.d("Account", "Got Writable database");
            // execute insert command

            ContentValues values = new ContentValues();
            values.put(Database.FRIENDS_NAME, editName.getText().toString());
            values.put(Database.FRIENDS_PHONE, editPhone.getText().toString());// "The error was displayed in the word Database"
            values.put(Database.FRIENDS_AREA, editArea.getText().toString());
            values.put(Database.FRIENDS_CITY, editCity.getText().toString());

            long rows = db.insert(Database.FRIENDS_TABLE_NAME, null, values);
            db.close();
            if (rows > 0) {
                Toast.makeText(this, "Added Friend Successfully!",
                        Toast.LENGTH_LONG).show();
                this.finish();
            } else
                Toast.makeText(this, "Sorry! Could not add Friend!",
                        Toast.LENGTH_LONG).show();

        } catch (Exception ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();//print message
        }

    }``
}

You did not share the DBHelper class, But with the available information, I think you should try DBHelper instead of Database in ALL the lines where you are using values.put().. 您没有共享DBHelper类,但是根据可用的信息,我认为您应该在使用values.put()的所有行中尝试使用DBHelper而不是Database。

 values.put(DBHelper.FRIENDS_NAME, editName.getText().toString());
 values.put(DBHelper.FRIENDS_PHONE, editPhone.getText().toString());
 values.put(DBHElper.FRIENDS_AREA, editArea.getText().toString());
 values.put(DBHelper.FRIENDS_CITY, editCity.getText().toString());

And also in insert method: 并且在插入方法中:

long rows = db.insert(DBHelper.FRIENDS_TABLE_NAME, null, values);

All the best 祝一切顺利

For the exception debug and verify SQL Script you are creating in Friendfinderactivity at line no 36. 对于异常调试和验证SQL脚本,请在第36行的Friendfinderactivity中创建。

The following is the query that would be created by your code: select * from SQLiteDatabasenull , which has wrong syntax. 以下是将由您的代码创建的查询: select * from SQLiteDatabasenull ,语法错误。

That's the issue. 这就是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM