为什么我的插入查询不起作用..?
Why my insert query is not working ..?
我是 Android 的新手。我想在此过程中将数据存储在 Android SQLite 中,我正在插入一些值,但它们没有插入。我在哪里做错了。我没有得到解决方案,请帮助我。
Mainacticity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10sp"
android:paddingRight="10sp"
tools:context="com.example.admin.justtrail.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="user name"
android:gravity="center"
android:id="@+id/loginusernameid"
android:layout_marginTop="10sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
android:gravity="center"
android:id="@+id/loginpasswordid"
android:layout_marginTop="10sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:onClick="logIn"
android:id="@+id/logid"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="signup"
android:id="@+id/signupid"
android:onClick="signup"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="select"
android:text="select"/>
</LinearLayout>
MainActivity.java:
package com.example.admin.justtrail;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
DatabaseHandler databaseHandler=new DatabaseHandler(MainActivity.this);
EditText loginusername,loginpassword;
String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginpassword = (EditText)findViewById(R.id.loginpasswordid);
loginusername = (EditText)findViewById(R.id.loginusernameid);
}
public void logIn(View view)
{
String logusername=loginusername.getText().toString();
String logpassword=loginpassword.getText().toString();
// Object usernaeme;
String data = databaseHandler.check(logusername,logpassword);
Log.v("sdfsdfsf",data);
}
public void signup(View view)
{
// String DATABASE_NAME="Chandu";
// String TABLE_NAME="fbdatabase";
Intent intent=new Intent(getApplicationContext(),Signup.class);
startActivity(intent);
/* File file = new File(String.valueOf(getDatabasePath(TABLE_NAME)));
boolean bb =file.isFile();
if(bb ==true)
{
Toast.makeText(getApplicationContext(),"table is created",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"table is not created",Toast.LENGTH_LONG).show();
}*/
}
public void select(View view)
{
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure you want see data?");
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1) {
data= databaseHandler.seclctquery();
Data da = new Data();
gotosecond();
}
});
alertDialogBuilder.show();
}
private void gotosecond()
{
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(data);
alertDialogBuilder.show();
}
}
signup.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.admin.justtrail.Signup">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:gravity="center"
android:id="@+id/nameid"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:gravity="center"
android:id="@+id/signupusernameid"
android:layout_marginTop="40sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/singuppasswordid"
android:layout_marginTop="80sp"
android:inputType="textPassword"
android:gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/signupsubmitid"
android:layout_marginTop="120sp"/>
</RelativeLayout>
Signup.java:
package com.example.admin.justtrail;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Signup extends AppCompatActivity {
EditText ename,eusername,epassword;
DatabaseHandler databaseHandler=new DatabaseHandler(Signup.this);
Button subbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
ename=(EditText)findViewById(R.id.nameid);
eusername=(EditText)findViewById(R.id.signupusernameid);
epassword=(EditText)findViewById(R.id.singuppasswordid);
subbutton=(Button)findViewById(R.id.signupsubmitid);
subbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Name=ename.getText().toString();
String Username=eusername.getText().toString();
String Password=epassword.getText().toString();
Data da=new Data();
da.setName(Name);
da.setUsername(Username);
da.setPassword(Password);
databaseHandler.addPersion(Name,Username,Password);
ename.setText("");
eusername.setText("");
epassword.setText("");
Toast.makeText(getApplicationContext(),"sign up success",Toast.LENGTH_LONG).show();
}
});
}
}
Data.java:
package com.example.admin.justtrail;
/**
* Created by admin on 9/16/2016.
*/
public class Data {
// Data da=new Data();
String Name;
String Username;
String Password;
public String getName() {
return Name;
}
public String getUsername() {
return Username;
}
public String getPassword() {
return Password;
}
public void setName(String name) {
Name = name;
}
public void setUsername(String username) {
Username = username;
}
public void setPassword(String password){
Password=password;
}
}
DatabaseHelper.java:
package com.example.admin.justtrail;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by admin on 9/15/2016.
*/
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME="Chandu";
private static final int DATABASE_VERSION=1;
private static final String TABLE_NAME="fbdatabase";
private static final String ID="ID";
private static final String NAME="NAME";
private static final String USER_NAME="USER_NAME";
private static final String PASSWORD="PASSWORD";
public DatabaseHandler(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query = "CREATE TABLE"+" "+ TABLE_NAME +"("+ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME +"TEXT,"+USER_NAME +"TEXT,"+PASSWORD +"VARCHAR"+");";
Log.v("sdfsdf", query);
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(sqLiteDatabase);
}
public void addPersion(String name,String user_name,String password)
{
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(NAME,name);
values.put(USER_NAME,user_name);
values.put(PASSWORD,password);
sqLiteDatabase.insert(TABLE_NAME,null,values);
sqLiteDatabase.close();
}
public String check(String USER_NAME,String PASSWORD)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
//String seclectquery = "SELECT "+USER_NAME +","+PASSWORD +" FROM"+" "+TABLE_NAME+";";
//Select * from database where username = '+USER_NAME+',password ='+PASSWORD+';
String seclectquery = "SELECT * FROM "+TABLE_NAME+" WHERE username="+USER_NAME+",password="+PASSWORD+";";
sqLiteDatabase.execSQL(seclectquery);
return seclectquery;
}
public String selectquery ( ) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String query1 = "SELECT * FROM"+" "+TABLE_NAME;
// sqLiteDatabase.execSQL(query1);
Cursor cursor = sqLiteDatabase.rawQuery(query1, null);
String line="";
int i =0;
if(cursor.moveToFirst())
{
do {
String s1 = cursor.getString(0);
String s2 = cursor.getString(1);
String s3 = cursor.getString(2);
String s4 = cursor.getString(3);
Log.v("asdasdadasdas",s1);
Log.v("asdasdadasdas",s2);
Log.v("asdasdadasdas",s3);
Log.v("asdasdadasdas",s4);
line = line+s1 + s2 + s3 + s4+"\n";
}
while (cursor.moveToNext());
}
return line;
}
}
第一个问题 到目前为止我遇到的是在创建 table 时列名和数据类型之间缺少 space。
改变这个
String query = "CREATE TABLE"+" "+ TABLE_NAME +"("+ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME +"TEXT,"+USER_NAME +"TEXT,"+PASSWORD +"VARCHAR"+");";
到
String query = "CREATE TABLE"+" "+ TABLE_NAME +"("+ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME +" TEXT,"+USER_NAME +" TEXT,"+PASSWORD +" VARCHAR"+");";
我是 Android 的新手。我想在此过程中将数据存储在 Android SQLite 中,我正在插入一些值,但它们没有插入。我在哪里做错了。我没有得到解决方案,请帮助我。
Mainacticity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10sp"
android:paddingRight="10sp"
tools:context="com.example.admin.justtrail.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="user name"
android:gravity="center"
android:id="@+id/loginusernameid"
android:layout_marginTop="10sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
android:gravity="center"
android:id="@+id/loginpasswordid"
android:layout_marginTop="10sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:onClick="logIn"
android:id="@+id/logid"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="signup"
android:id="@+id/signupid"
android:onClick="signup"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="select"
android:text="select"/>
</LinearLayout>
MainActivity.java:
package com.example.admin.justtrail;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
DatabaseHandler databaseHandler=new DatabaseHandler(MainActivity.this);
EditText loginusername,loginpassword;
String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginpassword = (EditText)findViewById(R.id.loginpasswordid);
loginusername = (EditText)findViewById(R.id.loginusernameid);
}
public void logIn(View view)
{
String logusername=loginusername.getText().toString();
String logpassword=loginpassword.getText().toString();
// Object usernaeme;
String data = databaseHandler.check(logusername,logpassword);
Log.v("sdfsdfsf",data);
}
public void signup(View view)
{
// String DATABASE_NAME="Chandu";
// String TABLE_NAME="fbdatabase";
Intent intent=new Intent(getApplicationContext(),Signup.class);
startActivity(intent);
/* File file = new File(String.valueOf(getDatabasePath(TABLE_NAME)));
boolean bb =file.isFile();
if(bb ==true)
{
Toast.makeText(getApplicationContext(),"table is created",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"table is not created",Toast.LENGTH_LONG).show();
}*/
}
public void select(View view)
{
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure you want see data?");
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1) {
data= databaseHandler.seclctquery();
Data da = new Data();
gotosecond();
}
});
alertDialogBuilder.show();
}
private void gotosecond()
{
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(data);
alertDialogBuilder.show();
}
}
signup.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.admin.justtrail.Signup">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:gravity="center"
android:id="@+id/nameid"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:gravity="center"
android:id="@+id/signupusernameid"
android:layout_marginTop="40sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/singuppasswordid"
android:layout_marginTop="80sp"
android:inputType="textPassword"
android:gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/signupsubmitid"
android:layout_marginTop="120sp"/>
</RelativeLayout>
Signup.java:
package com.example.admin.justtrail;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Signup extends AppCompatActivity {
EditText ename,eusername,epassword;
DatabaseHandler databaseHandler=new DatabaseHandler(Signup.this);
Button subbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
ename=(EditText)findViewById(R.id.nameid);
eusername=(EditText)findViewById(R.id.signupusernameid);
epassword=(EditText)findViewById(R.id.singuppasswordid);
subbutton=(Button)findViewById(R.id.signupsubmitid);
subbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Name=ename.getText().toString();
String Username=eusername.getText().toString();
String Password=epassword.getText().toString();
Data da=new Data();
da.setName(Name);
da.setUsername(Username);
da.setPassword(Password);
databaseHandler.addPersion(Name,Username,Password);
ename.setText("");
eusername.setText("");
epassword.setText("");
Toast.makeText(getApplicationContext(),"sign up success",Toast.LENGTH_LONG).show();
}
});
}
}
Data.java:
package com.example.admin.justtrail;
/**
* Created by admin on 9/16/2016.
*/
public class Data {
// Data da=new Data();
String Name;
String Username;
String Password;
public String getName() {
return Name;
}
public String getUsername() {
return Username;
}
public String getPassword() {
return Password;
}
public void setName(String name) {
Name = name;
}
public void setUsername(String username) {
Username = username;
}
public void setPassword(String password){
Password=password;
}
}
DatabaseHelper.java:
package com.example.admin.justtrail;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by admin on 9/15/2016.
*/
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME="Chandu";
private static final int DATABASE_VERSION=1;
private static final String TABLE_NAME="fbdatabase";
private static final String ID="ID";
private static final String NAME="NAME";
private static final String USER_NAME="USER_NAME";
private static final String PASSWORD="PASSWORD";
public DatabaseHandler(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query = "CREATE TABLE"+" "+ TABLE_NAME +"("+ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME +"TEXT,"+USER_NAME +"TEXT,"+PASSWORD +"VARCHAR"+");";
Log.v("sdfsdf", query);
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(sqLiteDatabase);
}
public void addPersion(String name,String user_name,String password)
{
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(NAME,name);
values.put(USER_NAME,user_name);
values.put(PASSWORD,password);
sqLiteDatabase.insert(TABLE_NAME,null,values);
sqLiteDatabase.close();
}
public String check(String USER_NAME,String PASSWORD)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
//String seclectquery = "SELECT "+USER_NAME +","+PASSWORD +" FROM"+" "+TABLE_NAME+";";
//Select * from database where username = '+USER_NAME+',password ='+PASSWORD+';
String seclectquery = "SELECT * FROM "+TABLE_NAME+" WHERE username="+USER_NAME+",password="+PASSWORD+";";
sqLiteDatabase.execSQL(seclectquery);
return seclectquery;
}
public String selectquery ( ) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String query1 = "SELECT * FROM"+" "+TABLE_NAME;
// sqLiteDatabase.execSQL(query1);
Cursor cursor = sqLiteDatabase.rawQuery(query1, null);
String line="";
int i =0;
if(cursor.moveToFirst())
{
do {
String s1 = cursor.getString(0);
String s2 = cursor.getString(1);
String s3 = cursor.getString(2);
String s4 = cursor.getString(3);
Log.v("asdasdadasdas",s1);
Log.v("asdasdadasdas",s2);
Log.v("asdasdadasdas",s3);
Log.v("asdasdadasdas",s4);
line = line+s1 + s2 + s3 + s4+"\n";
}
while (cursor.moveToNext());
}
return line;
}
}
第一个问题 到目前为止我遇到的是在创建 table 时列名和数据类型之间缺少 space。
改变这个
String query = "CREATE TABLE"+" "+ TABLE_NAME +"("+ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME +"TEXT,"+USER_NAME +"TEXT,"+PASSWORD +"VARCHAR"+");";
到
String query = "CREATE TABLE"+" "+ TABLE_NAME +"("+ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME +" TEXT,"+USER_NAME +" TEXT,"+PASSWORD +" VARCHAR"+");";